我想写一个新的工作簿,有一张表,并在该表中添加一个表。 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))