简体   繁体   English

使用 Pandas ExcelWriter 创建的 xlsx 文件需要修复

[英]xlsx file created with Pandas ExcelWriter requires repair

As stated in the title, the xlsx created with the code below is corrupt.如标题中所述,使用以下代码创建的 xlsx 已损坏。 Can anyone see a problem in my code?任何人都可以在我的代码中看到问题吗?

if "O-" in project:
    path = project_folder_order
elif "Q-" in project:
    path = project_folder_quote

book = load_workbook(path)

writer = pd.ExcelWriter(str(path), engine='openpyxl', mode='a')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
worksheet = writer.sheets["Create"]

worksheet.cell(row=int(no_target_lang_len) + 1, column=1).fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')                                                                          

df1 = pd.read_csv(r"\\awsdfpudblapp.xxx.de\Planet\Krake\\" + project + "\\4_Term_TO_DO_" + project + ".csv")
df1_create = df1["Create"]
df1_search = df1["Search"]

df1_create.to_excel(writer, sheet_name="Create", index=False, startcol=0, startrow=1, merge_cells=True)
df1_search.to_excel(writer, sheet_name="Search", index=False, startcol=0, startrow=no_target_lang_len, merge_cells=True)

worksheet.merge_cells(start_row=int(no_target_lang_len) + 1, start_column=1, end_row=int(no_target_lang_len) + 1, end_column=4)

writer.save()
writer.close()

The Excel error message is rather vague: Excel 错误消息相当含糊:

error052160_01.xml Errors were detected in file 'D:\Users\xxx\4_Term_TODO_Q-21-001727-01.xlsx' Excel completed file level validation and repair. error052160_01.xml 在文件“D:\Users\xxx\4_Term_TODO_Q-21-001727-01.xlsx”中检测到错误 Excel 已完成文件级验证和修复。 Some parts of this workbook may have been repaired or discarded.本工作簿的某些部分可能已被修复或丢弃。

After letting Excel repair the file, the content does appear, but I need to save it as another version in order to be able to use the file in another application.让Excel修复文件后,内容确实出现了,但我需要将其另存为另一个版本,以便能够在另一个应用程序中使用该文件。

I also have the problem with this somewhat shorter code:我也有这个稍微短一点的代码的问题:

if "O-" in project:
    path = project_folder_order
elif "Q-" in project:
    path = project_folder_quote

book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl', mode='a')
writer.book = book

df1 = pd.read_csv(r"\\awsdfpudblapp.xxx.de\Planet\Krake\\" + project + "\\4_Term_TO_DO_" + project + ".csv")
df1.to_excel(writer, sheet_name="Term candidates", index=False, merge_cells=True)


writer.save()
writer.close()

I faced this problem before.我以前遇到过这个问题。

In your last line of code.在你的最后一行代码中。 Just change writer.close() to book.close() .只需将writer.close()更改为book.close() It works fine in my code.它在我的代码中运行良好。 May cause from load_workbook and you didn't close them.可能是由load_workbook引起的,而您没有关闭它们。

Instead of using the last lines of your code:而不是使用代码的最后几行:

writer.save()
writer.close()

Try to use:尝试使用:

book.save()

My opinion: when you are saving the writer object it is only some part of excel and it is advised to keep the whole excel file, so the book object as an entire excel should save excel without any damage.我的意见:当你在保存writer object时,它只是excel的一部分,建议保留整个excel文件,所以book作为整个excel应该保存excel而没有任何损坏。

Had repair file issue also.In my case it was pandas.也有修复文件问题。就我而言,它是熊猫。 I downgrade to 1.1.3 and it worked fine.我降级到 1.1.3 并且运行良好。 (openpyxl==3.0.5) (openpyxl==3.0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM