2021年3月23日星期二

Append dataframe to Excel table with formatting on same sheet

I currently have an Excel table with formatting (see the image below). I'm trying to append a new pandas dataframe to the same table underneath. I tried using pd.concat() and writing it out to a new excel file, but I lose all the formatting this way. I also tried using codes similar to the one below, but this writes the data out to a new sheet within the same excel workbook, not on to the same table. Is what I'm trying to do actually possible? Been stuck on it for hours so appreciate any help.

with pd.ExcelWriter('Leads (Schools and Education).xlsx', engine="openpyxl", mode="a") as file:

  new_df.to_excel(file, sheet_name="School_Lead_List", startrow=num_rows+1, index=False)  

Note: The column names are the same, although the number of columns are different in the table and the dataframe (I can create dummy columns in the new dataframe to match if necessary).

Original excel table with formatting

Dataframe I'm trying to append

https://stackoverflow.com/questions/66773382/append-dataframe-to-excel-table-with-formatting-on-same-sheet March 24, 2021 at 09:07AM

没有评论:

发表评论