Tables added with openpyxl are not found by calamine. When saved with other applications, the relationship target has a relative path, rather than the absolute path produced with openpyxl. Changing /xl/ to ../ means the table is then found by calamine. This is demonstrated with the following:
from io import BytesIO
from zipfile import ZipFile
from fastexcel import read_excel
from openpyxl import Workbook, worksheet
def _get_tables(b):
b.seek(0)
return read_excel(b.read()).table_names()
# Create example workbook with table
b = BytesIO()
wb = Workbook()
ws = wb.active
ws.append(["A"])
ws.append([1])
ws.add_table(worksheet.table.Table(displayName="table", ref="A1:A2"))
wb.save(b)
# Fetch tables for example workbook: none are found
tables = _get_tables(b)
# Modify example workbook
c = BytesIO()
with ZipFile(b, "r") as original, ZipFile(c, "w") as corrected:
for zi in original.infolist():
f = original.open(zi)
data = f.read()
if zi.filename == "xl/worksheets/_rels/sheet1.xml.rels":
print("openpyxl output:")
print(data)
print("openpyxl tables: ", tables)
data = data.replace(b"/xl/tables", b"../tables")
print("\ncorrected output:")
print(data)
corrected.writestr(zi.filename, data)
# Fetch tables for modified workbook: table is found
print("corrected tables: ", _get_tables(c))
which outputs the following:
openpyxl output:
b'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="/xl/tables/table1.xml" Id="rId1" /></Relationships>'
openpyxl tables: []
corrected output:
b'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.xml" Id="rId1" /></Relationships>'
corrected tables: ['table']
This may be a bug in the way openpyxl creates an absolute path - I tried to open an issue there, but it was flagged as spam. I don't know the specification well enough to be sure: is the target changed when replacing /xl/ with ../?
Tables added with openpyxl are not found by calamine. When saved with other applications, the relationship target has a relative path, rather than the absolute path produced with openpyxl. Changing
/xl/to../means the table is then found by calamine. This is demonstrated with the following:which outputs the following:
This may be a bug in the way openpyxl creates an absolute path - I tried to open an issue there, but it was flagged as spam. I don't know the specification well enough to be sure: is the target changed when replacing
/xl/with../?