[Python] openpyxl corrupting xls file after adding new Table to a sheet

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently trying to generate an excel table within a new sheet in a workbook. I'm able to add a new sheet and save it with some random data in a given cell, but when I try to add a new Table, if I save it then I get a corruption error when loading the file. This is a simple test function I've made that creates a sheet, and tries to add a new table to it. If I comment out the sheet.add_table(table) line then there is no corruption.

    def tabletest(workbookName, sheetName):
    workbook = openpyxl.load_workbook(workbookName, data_only=True)
    sheet = workbook.create_sheet(title=sheetName)
    sheet['A1'] = 'Part Number'
    # # Add table with required style info
    table = Table(displayName="Tbl_1", ref="A1:G6")
    table.tableStyleInfo = TableStyleInfo(
    name="TableStyleMedium2",
    showRowStripes=True,
    showColumnStripes=False,
    showFirstColumn=False,
    showLastColumn=False
    )
    sheet.add_table(table)
    workbook.save(workbookName)
    workbook.save(workbookName)
    workbook.close() # Make sure to close the workbook


    I get the following message upon repairing.


    Repaired Records: Table from /xl/tables/table26.xml part (Table)
    ​

    Note that the new table does show up in the Name Manager even after the repairs. Has anybody else experienced this issue and found a fix?

    Continue reading...

Compartilhe esta Página