gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:06:39 29 4
gpt4 key购买 nike

如标题所述,使用以下代码创建的 xlsx 已损坏。任何人都可以在我的代码中看到问题吗?

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()

Excel 错误信息比较模糊:

error052160_01.xml在文件“D:\Users\xxx\4_Term_TODO_Q-21-001727-01.xlsx”中检测到错误Excel 完成了文件级别的验证和修复。本工作簿的某些部分可能已被修复或丢弃。

让 Excel 修复文件后,内容确实出现了,但我需要将其保存为另一个版本,以便能够在另一个应用程序中使用该文件。

我也有这个有点短的代码的问题:

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()

最佳答案

我以前遇到过这个问题。

在您的最后一行代码中。只需将 writer.close() 更改为 book.close()。它在我的代码中运行良好。可能来自 load_workbook 而你没有关闭它们。

关于python - 使用 Pandas ExcelWriter 创建的 xlsx 文件需要修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68663237/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com