TL;DR;
- 使用 Openpyxl 保存对大型 Excel 文件的更改会导致 xlsx 文件损坏
- Excel 文件由多个带有图形、公式、图像和表格的选项卡组成。
- Powershell 脚本可以毫无问题地将编辑保存到 xlsx 文件。
- 我可以使用 Openpyxl 从 Excel 文件中读取单元格值,我还可以手动编辑和保存 xlsx 文件。
- Excel 文件未受保护。
- 下面提供了所有错误和代码片段。
我无法将数据添加到我们的一个团队正在使用的 Excel 文件中。 Excel 文件相当大(+3MB),有几张表,包含公式和图表,还有图像。
值得庆幸的是,我需要输入数据的工作表没有这些,但是,我发现当我尝试保存工作簿时,我最终遇到了这些错误:
Traceback (most recent call last):
File "test.py", line 5, in <module>
wb.save("new.xlsx")
File "C:\Python3\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Python3\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\Python3\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Python3\lib\site-packages\openpyxl\writer\excel.py", line 78, in write_data
self._write_charts()
File "C:\Python3\lib\site-packages\openpyxl\writer\excel.py", line 124, in _write_charts
self._archive.writestr(chart.path[1:], tostring(chart._write()))
File "C:\Python3\lib\site-packages\openpyxl\chart\_chart.py", line 134, in _write
return cs.to_tree()
File "C:\Python3\lib\site-packages\openpyxl\chart\chartspace.py", line 193, in to_tree
tree = super(ChartSpace, self).to_tree()
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 154, in to_tree
node = obj.to_tree(child_tag)
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 154, in to_tree
node = obj.to_tree(child_tag)
File "C:\Python3\lib\site-packages\openpyxl\chart\plotarea.py", line 135, in to_tree
return super(PlotArea, self).to_tree(tagname)
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 146, in to_tree
for node in nodes:
File "C:\Python3\lib\site-packages\openpyxl\descriptors\sequence.py", line 105, in to_tree
el = v.to_tree(namespace=namespace)
File "C:\Python3\lib\site-packages\openpyxl\chart\_chart.py", line 107, in to_tree
return super(ChartBase, self).to_tree(tagname, idx)
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 146, in to_tree
for node in nodes:
File "C:\Python3\lib\site-packages\openpyxl\descriptors\sequence.py", line 39, in to_tree
el = v.to_tree(tagname, idx)
File "C:\Python3\lib\site-packages\openpyxl\chart\series.py", line 170, in to_tree
return super(Series, self).to_tree(tagname)
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 154, in to_tree
node = obj.to_tree(child_tag)
File "C:\Python3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 154, in to_tree
node = obj.to_tree(child_tag)
AttributeError: 'str' object has no attribute 'to_tree'
这是我用来执行“另存为”过程的代码,所以不要介意添加数据之类的,保存操作会损坏文件:
from openpyxl import load_workbook
wb=load_workbook("Production Monitoring Script.xlsx")
ws=wb['Prod Perf Script Data']
wb.save("new.xlsx")
我尝试了使用 Powershell 的替代解决方案,它起作用了。
$xl=New-Object -ComObject Excel.Application
$wb=$xl.WorkBooks.Open('<path here>\Production Monitoring Script.xlsx')
$ws=$wb.WorkSheets.item(1)
$xl.Visible=$true
$ws.Cells.Item(7, 618)=50
$wb.SaveAs('<path here>\New.xlsx')
$xl.Quit()
它能够在该单元格中保存值“50”。