运行python脚本后的excel恢复错误

时间:2018-12-14 13:06:36

标签: python excel recovery

我正在运行python脚本,将记事本中的某些值粘贴到excel。 excel的地址部分应该在不同的行中,如下所示: Python Excel Error

但是,这不会像上面那样在excel中打印值。而是这样打印: Python Excel Error 1

下面是代码: **

**# -*- coding: utf-8 -*-
"""
Created on Fri Nov  2 12:00:36 2018
@author: yy
"""
import re
import openpyxl
name = "Default"
i = 2
add = ""
text = [line.rstrip('\n') for line in open(r'''C:\Users\xxx\Desktop\yy files\EDF Energy\NewNote.txt''', "r")]
book = openpyxl.load_workbook(r'C:\Users\xxx\Desktop\yy files\test_value.xlsx')
sheet = book['Sheet1']
#ws = book.worksheets[0]
        #Retriving Account Number
for line in text: 
    x = re.findall('(\d{3}[\s]\d{3}[\s]\d{3}[\s]\d{3})', line)
    if len(x) != 0: 
        accno= (", ".join(x))
        y= accno.replace(" ","")
        accno=int(y)
        print("account number, y ",accno, y)
#Retriving Name      
    if "Hello" in line:
        name = line[5:]
        name= name.replace(",","")
        name= name.replace("-","")
        print("name ",name)
        #sheet['B2']=name
 #Retriving outstanding Balance
    if "projection:" in line:
        z = re.findall(r'\d+[.]\d+',line)
        print("z ",z)
        accbal = (", ".join(z))
        print("account balance ",accbal)

        #sheet['D2']= (", ".join(z))


   # Retriving Address details
addlines=[]
totalline=0
dearwordline=0
fstnameline=0
index=0
account_index=0
add_index=0


for y in text:
    if "Page 1 of " in y:
        fstnameline=index
        print("fstnameline ",fstnameline)
        break
    index+=1
for z in text:
    if "personal projection:" in z:
        dearwordline=totalline
        print("dearwordline",dearwordline)
        print("totalline",totalline)
    totalline+=1
for cell in sheet["C"]:
    if cell.value is None:
            sheet.cell(column= 2, row=cell.row+account_index, value= name)
            sheet.cell(column= 4, row=cell.row+account_index, value= accbal)
            sheet.cell(column= 1, row=cell.row+account_index, value= accno)
            account_index = dearwordline-fstnameline+1
            #account_name+=account_index
            break
for cell in sheet["C"]:
    if cell.value is None:
        for x in text[fstnameline+1:dearwordline]:
            if add_index < dearwordline-fstnameline-1 :
                if add_index <= cell.row :
                    x.strip()
                    x= x.replace("Could you pay less?","")
                    x= x.replace(",",";")
                    print("x in text:",x)
                    print("cell.row in C : ",cell.row)
                    x= x.replace(" Over the next 12 months","")
                    sheet.cell(row = cell.row, column =3,value = x)
                    print("x after printing in excel:",x)
                    #if cell.row == 7:
                        #print(cell.row,"+" , add_index)
                    cell.row+=1
    #                add_index = dearwordline-fstnameline-1
                    add_index+=1


    #sheet.cell(row = i, column =3).value = x
    #add = x(i) 
    #i+=1

"""for cell in ws["C"]:
    if cell.value is text[fstnameline+1:dearwordline]:
        print("cell row for A,B,D : ",cell.row)
       # ws.cell(column= 1, row=cell.row, value= accno)
        ws.cell(column= 2, row=cell.row, value= name)
        ws.cell(column= 4, row=cell.row, value= accbal)
        i= cell.row
        print (i)
            #break
        #for cell in ws["C"]:
         #   if cell.value is None:
            #ws.cell(row = cell.row, column =3,value = x)
                #ws.cell(column= 3, row=cell.row, value= x)
        cell.row+=1 
        print("cell.row",cell.row)
        print("x in C column ",x)
            #break"""


   # print (add)




    #Writing address to excel
    #sheet.cell(row = i, column =3).value = x
    #i+=1       


        #sheet['Ai'] = int(y)


book.save(r'C:\Users\xxx\Desktop\yy files\test_value.xlsx')**

**

有人可以帮忙吗? 我对此无能为力。 我只有python的基本知识。当我尝试在新的excel文件中写入值时,此代码工作异常,它不在循环本身内部。 而且它不是在Excel中编写值。

运行python脚本后,我得到了excel恢复错误,如下所示: Excel Recovery Pop up Excel Recovery Pop up 1

对不起,我的图像链接,因为Stackoverflow网站不允许我插入图像。所以请任何人帮助我。

0 个答案:

没有答案