简体   繁体   中英

Append a pandas dataframe to an existing excel table

I need some help with the following. I currently use python pandas to open a massive spreadsheet every day (this spreadsheet is a report, hence every day the data inside the spreadsheet is different). Pandas dataframe allows me to quickly crunch the data and generate a final output table, with much less data than the initial excel file. Now, on day 1, I would need to add this output dataframe (3 rows 10 columns) to a new excel sheet (let's say sheet 1).

On day 2, I would need to take the new output of the dataframe and append it to the existing sheet 1. So at the end of day 2, the table in sheet1 would have 6 rows and 10 columns.

On day 3, same thing. I will launch my python pnadas tool, read data from the excel report, generate an output dataframe 3x10 and append it again to my excel file.

I can't find a way to append to an existing excel table. Could anybody help? Many thanks in advance, Andrea

If you use openpyxl's utilities for dataframes then you should be able to do everything you need with the existing workbook, assuming this fits into memory.

from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = load_workbook("C:\Andrea\master_file.xlsx")
ws = wb[SHEETNAME]
for row in dataframe_to_rows(dt_today):
    ws.append(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM