2

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

2
  • You just need to change the ref on the table. Commented Mar 20, 2019 at 14:56
  • I tried it. How to deal with style then? I need to keep the formatting. Commented Mar 20, 2019 at 16:54

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

0

This has been very useful for me and it is adapted so you can add multiple rows from a pandas DataFrame at once. The table headers must coincide with the table dataframe column names.

from openpyxl import load_workbook
def add_df_data_to_existing_excel_table(excel_file, sheet_name, table_name, df_new_data):
    """
    Add a DataFrame to an existing Excel table. The DataFrame must have the same columns as the table.
    
    parameters
    ----------
    excel_file: str, path to the Excel file
    sheet_name: str, name of the sheet
    table_name: str, name of the table
    df_new_data: DataFrame, data to be added to the table
    """
    wb=load_workbook(excel_file)
    table = wb[sheet_name].tables[table_name]
    row_num = wb[sheet_name].max_row + 1
    column_key = {name: index+1 for index, name in enumerate(table.column_names)}

    for d in df_new_data.to_dict(orient='records'):
        for col, value in d.items():
            wb[sheet_name].cell(row=row_num, column=column_key[col]).value = value
        row_num += 1

    table.ref = table.ref[:-1] + str(row_num)  # A1:W98
    wb.save(excel_file)  

1 Comment

Doesn't this assume the table starts at column A?
-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}"

2 Comments

Please provide some context to your answer so that others can easily understand you approach to solve the problem.
Please explain what the code does and how it does it.
-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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.