gpt4 book ai didi

python - openpyxl - 只写工作表无法添加表

转载 作者:行者123 更新时间:2023-12-04 21:07:52 27 4
gpt4 key购买 nike

我想写一个新的工作簿,一张纸,然后在这张纸上添加一个表格。 AFAIK 应该是可能的,这个提交应该做到 https://bitbucket.org/openpyxl/openpyxl/commits/6f440b0ed398

但是当我尝试这样做时,它会失败并显示以下消息:

KeyError: 'Unknown relationship: None'

我的代码片段是:
from openpyxl import Workbook
from openpyxl.worksheet.table import Table

wb = Workbook(write_only=True)
ws = wb.create_sheet()

ws.append(['col1','col2','col3','col4'])
ws.append(['val11','val12','val13','val14'])
ws.append(['val21','val22','val23','val24'])
ws.append(['val31','val32','val33','val34'])
ws.append(['val41','val42','val43','val44'])

table = Table(displayName="basetable", ref="A1:D4")
ws.add_table(table)
wb.save('table.xlsx')

完整的追溯是:
File "<stdin>", line 1, in <module>
wb.save('table.xlsx')

File "./site-packages/openpyxl/workbook/workbook.py", line 354, in save
save_dump(self, filename)

File "./site-packages/openpyxl/writer/excel.py", line 313, in save_dump
writer.save(filename)

File "./site-packages/openpyxl/writer/excel.py", line 266, in save
self.write_data()

File "./site-packages/openpyxl/writer/excel.py", line 83, in write_data
self._write_worksheets()

File "./site-packages/openpyxl/writer/excel.py", line 229, in _write_worksheets
ws._rels[t._rel_id].Target = t.path

File "./site-packages/openpyxl/packaging/relationship.py", line 97, in __getitem__
raise KeyError("Unknown relationship: {0}".format(key))

最佳答案

Question: write-only worksheet fails to add table



试过你的例子得到:
AttributeError: 'WriteOnlyWorksheet' object has no attribute 'add_table'

已删除 write_only=True ,工作正常。

Output:
enter image description here



使用 Python 测试:3.4.2 和 2.7.9 - openpyxl:2.4.1 - LibreOffice:4.3.3.2

关于python - openpyxl - 只写工作表无法添加表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847102/

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