-1

This is an old question, however no one wrote a solution for without loading the excel file. Assuming the excel file is too huge, this will be inefficient to load the existing file and save on it. Instead is there any method to append, like for ordinary files?

df is the dataframe I would like to append

df.to_excel(folder+"file.xlsx", header=False, index=False)

However file.xlsx exists and there is another df with the same headers.

How can I append df to existing file.xlsx, without loading file.xlsx?

  • Hi you can may be check this post if you haven't already looked https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas – Ashwini Aug 26 '19 at 09:58
  • I took a look:"# try to open an existing workbook writer.book = load_workbook(filename)" I dont need to load the existing file. –  Aug 26 '19 at 11:02
  • If you want to append to an existing worksheet, you must load it into memory. There is no way to avoid this. – Charlie Clark Aug 27 '19 at 07:58

1 Answers1

0

I do not know about any append features in Openpyxl. Openpyxl has however a writeonlymode wb = Workbook(write_only=True)

I would try to save (and append) the dataframe as a feather binary file, then just delete and create a new workbook with write_only mode.

This should be much faster than loading and appending the Excel file, but it will depend on what type and how much data there is.

Hope this helps.

  • yes, that is what I was thinking. If no solution found I will follow this one. –  Aug 27 '19 at 04:25
  • You could also try to append directly to the excel file. But since there are no (as far as I know) solutions out there, you would have to store it as a zip without compression (STORED mode). Then you might be able to read the zip file backwards until you reach the xml part and insert the data. But it will be much more tricky to implement. – Anders Steen Nilsen Aug 27 '19 at 07:54