openpyxl无法保存到文件

时间:2019-12-17 13:50:45

标签: python openpyxl

我是Python的新手,openpyxl软件包遇到了一个非常令人困惑的问题。

每次我尝试将工作簿保存到文件时,都会出现错误: TypeError:输入类型,预期字符串或Element的输入值无效

即使对于非常简单直接的代码,我也会遇到相同的错误:

wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'test'
ws['A1'].value = 'test'
ws['A2'].value = 'design'
wb.save("D:\\PyData\\test\\test.xlsx")

此“ text.xlsx”文件将被创建,但已损坏,尽管我可以将其解压缩以查看3个文件,但Microsoft Excel无法打开它。

这是完整的追溯:

File "D:/PyProjects/ListingGen/load_img2xls.py", line 81, in <module>
test_save_xls()
File "D:/PyProjects/ListingGen/load_img2xls.py", line 17, in test_save_xls
wb.save("D:\\PyData\\test\\test.xlsx")
File "C:\Python38\lib\site-packages\openpyxl\workbook\workbook.py", line 408, in save
save_workbook(self, filename)
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data
self._write_worksheets()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet
writer.write()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 354, in write
self.write_top()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 98, in write_top
self.write_properties()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 60, in write_properties
self.xf.send(props.to_tree())
File "C:\Python38\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

我认为我可能错过了一些东西,但确实找不到。我在论坛上搜索,但似乎没有人遇到相同的问题。

任何人都可以给我一些提示吗?

非常感谢

0 个答案:

没有答案