Manipulate existing excel table using openpyxl

前端 未结 3 1575
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 08:57

I\'m currently honing my python / excel skills, and have run into an issue with openpyxl

I am trying to open a workbook, replace rows in an existing table, and save

3条回答
  •  天涯浪人
    2020-12-18 09:33

    First of all, thank you for this thread. I try to extend an existing table within an existing excel file (copy of a template). I simply fail to extend the table to the range i actually put data in (Remark: Some table elements contain formulas which i need to preserve)

    What i do is, open the excel file, copy and paste data to the correct worksheet and the correct cells. That works as intended. What does not work is extending the range of the table which initially covers only the first row (apart from the header).

    Using above code, i am able to identify the table and i tried copying the style:

    for i, table in enumerate(ws._tables):
            if table.name == 'Template':
                tableRef = i
                mystyle = table.tableStyleInfo
    resTable = Table(displayName="Template", ref="A7:{}{}".format(colnum_string(maxRef[1]), maxRef[0]))
    resTable.tableStyleInfo = mystyle
    ws._tables[tableRef] = resTable
    

    I might overlook something here since it does not work. The table does not extend. Any help is greatly appreciated.

    For better understanding of the problem:
    Table header is A7:BA7
    First (empty) row, some element with formulas and formatting: A8:BA8
    Final data range after copying data (as example, calculation is correct): A8:BA255

提交回复
热议问题