1

I'm working with the .xlsx file and it has a tab with a workhsheet table where lots of conditional formatting are used. From time to time I need to append this table with new rows. My plan is to use python openpyxl (or other package) to append this table. so far I could identify this table as

from openpyxl import load_workbook
wb=load_workbood(myfile)
ws=wb['mytab']
tab = wb.ws._tables[0]

Can I use something like .append() method or change data of this table to add more rows to it? My goal is to keep the formatting.

I've already tried this approach - Manipulate existing excel table using openpyxl and it doesn't' work for me

I'm using openpyxl 2.6.1

Regards, Pavel

Pavel
  • 11
  • 1
  • 2

3 Answers3

0

update "table.ref" to max row

# ws - work sheet
row_num = ws.max_row + 1
table.ref[:-1] + str(row_num)  # A1:W98

add new row data

row_data = [1,2,3,4,5,]
for col, value in enumerate(row_data):
    ws.cell(row=row, column=col+1).value = value
anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
-1
from openpyxl import load_workbook
wb=load_workbood(myfile)
ws=wb['mytab']
tab = ws.tables["Table1"]
tab.ref = f"A1:{ws.max_column}{ws.max_row}"
cauchy
  • 1
-2
from openpyxl import load_workbook

filename= r'C:\Users\PC/test.xlsx'

wb = load_workbook(filename)
ws = wb['Hoja1']
ws["A1"] = "AAA"
ws["A2"] = "BBB"

wb.save(filename)
David Buck
  • 3,752
  • 35
  • 31
  • 35