gpt4 book ai didi

python - Openpyxl Workbook.save 函数创建损坏且无法打开的 Excel (.xlsx) 文件

转载 作者:行者123 更新时间:2023-12-01 00:16:21 32 4
gpt4 key购买 nike

我尝试过使用August William's solution到这个问题,但这也不起作用。我没有切换工作簿类型,即 .xlsm 到 .xlsx,这似乎是一个单独的问题。我浏览过Openpyxl's Manual试图找到错误报告或错误修复,但无济于事。下面是我非常简单的代码。接下来是 python 错误消息,该消息导致创建工作簿,但该工作簿已损坏且无法加载。任何帮助表示赞赏。-谢谢!!

from openpyxl import Workbook


dashbrd = Workbook()
fp = dashbrd.active
fp.title = 'Sheet Name Goes Here'

fp['A1'] = 'Header'
fp['B1'] = '2nd Header'
fp['C1'] = '3rd Header'
fp['D1'] = '4th Header'
fp['E1'] = '5th Header'
fp['F1'] = 'You get the idea'
fp['G1'] = 'Another Header'
fp['H1'] = 'Blah blah blah'
fp['I1'] = 'Yadda yadda yadda'

dashbrd.save("S:\\folder1\\folder2\\folder3\\MyBook.xlsx")


**************************************************************************************
Traceback (most recent call last):

File "C:\Users\NotaDirtyUser\Documents\Scripts\HeaderTest.py", line 26, in <module>
dashbrd.save("S:\\folder1\\folder2\\folder3\\MyBook.xlsx")
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 408, in save
save_workbook(self, filename)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data
self._write_worksheets()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet
writer.write()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\worksheet\_writer.py", line 354, in write
self.write_top()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\worksheet\_writer.py", line 98, in write_top
self.write_properties()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\worksheet\_writer.py", line 60, in write_properties
self.xf.send(props.to_tree())
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\worksheet\_writer.py", line 294, in get_stream
xf.write(el)
File "src/lxml/serializer.pxi", line 1652, in lxml.etree._IncrementalFileWriter.write
TypeError: got invalid input value of type <class 'xml.etree.ElementTree.Element'>, expected string or Element

最佳答案

就像我上面说的,昨天我遇到了同样的问题..我在此链接中找到了解决方案:

https://python-forum.io/Thread-Need-help-in-understanding-this-particular-Traceback-TypeError

引用此错误:类型错误:获取类型、预期字符串或元素的无效输入值

总而言之,解决方案是将 openpyxl 安装到另一个版本:

pip uninstall openpyxl
pip install openpyxl==3.0.1

关于python - Openpyxl Workbook.save 函数创建损坏且无法打开的 Excel (.xlsx) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308064/

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