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

问题描述 投票:1回答:2

我曾尝试使用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
python openpyxl
2个回答
0
投票

我找不到很好的参考,但我记得偶然发现了相同的参考,解决方案是改用旧格式(.xls,这是一种完全不同的格式)。似乎是openpyxl的一般问题,目前尚未解决。

附加到.xlsx的有效方法(对我有用:

from openpyxl import load_workbook
writer = pd.ExcelWriter(filename, engine='openpyxl')

try:
    # try to open an existing workbook
    writer.book = load_workbook(filename)

    # get the last row in the existing Excel sheet
    # if it was not specified explicitly
    if startrow is None and sheet_name in writer.book.sheetnames:
        startrow = writer.book[sheet_name].max_row

    # truncate sheet
    if truncate_sheet and sheet_name in writer.book.sheetnames:
        # index of [sheet_name] sheet
        idx = writer.book.sheetnames.index(sheet_name)
        # remove [sheet_name]
        writer.book.remove(writer.book.worksheets[idx])
        # create an empty sheet [sheet_name] using old index
        writer.book.create_sheet(sheet_name, idx)

    # copy existing sheets
    writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
except FileNotFoundError:
    # file does not exist yet, we will create it
    pass

if startrow is None:
    startrow = 0

# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)

# save the workbook
writer.save()

0
投票

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

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

关于此错误:TypeError:输入的类型,预期字符串或Element的输入值无效

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

pip uninstall openpyxl
pip install openpyxl==3.0.1
© www.soinside.com 2019 - 2024. All rights reserved.