gpt4 book ai didi

python - 尝试使用openpyxl python修改1个单元格时,Excel文件已损坏。

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:29 32 4
gpt4 key购买 nike

我正在尝试使用openpyxl在启用宏的excel 2013文件中更改1个单元格。这应该是一个相对容易的任务,我可以使用以下代码,但是由于某些原因,当我稍后尝试打开修改后的excel文件时,我从excel中收到了错误消息。

“我们发现您想恢复“工作簿”中某些内容的问题吗?”

如果我恢复文件,它会从文件中删除很多内容,但是我仍然可以看到,我要修改的唯一单元格具有正确的数据。

当我打开错误日志时,收到以下消息:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error086600_01.xml</logFileName>
<summary>Errors were detected in file 'C:\Files\2018 Workflow\Stress Due Date (NA) --- TR9999999 Pckg#9 - Emergency Equipment - 9 - 9 - 9\Panel Analysis --- 9 Version A.xlsm'</summary>
-<repairedParts summary="Following is a list of repairs:">
<repairedPart xml:space="preserve">Repaired Part: /xl/worksheets/sheet1.xml part. </repairedPart>
</repairedParts>
</recoveryLog>


以下是我的python代码,我已经阅读了文档,似乎应该可以使用,但是为什么会损坏我的文件?

""" 
The next part of the script will modify the stress panel excel documents
"""
wb = load_workbook(filename = os.path.join(new_path,new_panel + excel_ext), read_only = False, keep_vba = True)
ws = wb.get_sheet_by_name("Adapter_Panel_Tool")
ws['C2'] = "Adapter Panel Quick Stress Analyses Tool - " + packet_list['Installations list'][0]
wb.save(os.path.join(new_path,new_panel + " Version A" + excel_ext))
wb.close()


#checks if there are 2 files: the filled one and the empty one and deletes the empty if it exist.
is_file_filled_out = os.path.isfile(os.path.join(new_path,new_panel + " Version A" + excel_ext))
is_there_empty_file = os.path.isfile(os.path.join(new_path, new_panel + excel_ext))
if is_file_filled_out == True and is_there_empty_file == True:
os.remove(os.path.join(new_path,new_panel + excel_ext))
print("\n\n The following file will be remove: \n" + new_panel + excel_ext + "\n\n" )
print("The prefilled template has been created: \n " + new_path + new_panel + excel_ext)

最佳答案

首先,尝试以安全模式打开Excel文件。如果它将打开,则您将在COM加载项上工作。尝试删除它们,然后打开文件。

关于python - 尝试使用openpyxl python修改1个单元格时,Excel文件已损坏。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53326195/

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