Can't save a write-only workbook with worksheet tables.
Created originally on Bitbucket by Tim
I want to add a table to a write-only workbook, but upon saving the workbook, I get this error:
TypeError: 'WriteOnlyWorksheet' object is not subscriptable
The bug happens in /openpyxl/worksheet/_writer.py
in line 269 in the method write_tables(self)
:
row = self.ws[table.ref][0]
The docs don't state that it wouldn't be possible to add tables to a write-only workbook, so I guess it's a bug then.
This is the code I'm using:
wb = openpyxl.Workbook(write_only=True)
ws: openpyxl.worksheet.worksheet.Worksheet = wb.create_sheet()
df = pandas.DataFrame(data)
for row in openpyxl.utils.dataframe.dataframe_to_rows(df, index=False, header=True):
ws.append(row)
ref = _get_ref(df) # some function to obtain a valid excel reference
table = openpyxl.worksheet.table.Table(displayName='tbl', ref=ref)
table.tableStyleInfo = openpyxl.worksheet.table.TableStyleInfo(name="TableStyleLight1", showFirstColumn=False, showLastColumn=False, showRowStripes=True, showColumnStripes=False)
ws.add_table(table)
wb.save('test.xlsx')
Is there any way to work around this?
I'm using openpyxl v3.0.3