14

Even using the code below, the content on each sheet of the .xlsx file is overwritten, not appended. What is missing?

writer = pd.ExcelWriter(excelfilepath, engine='openpyxl', mode='a', if_sheet_exists='overlay')

df1.to_excel(writer, sheet_name='Núcleo de TRIAGEM')

df2.to_excel(writer, sheet_name='Núcleo de FALÊNCIAS')

df3.to_excel(writer, sheet_name='RE - Triagem')

df4.to_excel(writer, sheet_name='RE - Falências')

writer.save()
BigBen
  • 46,229
  • 7
  • 24
  • 40
Victor Faraon
  • 151
  • 1
  • 4
  • any luck with this issue? did you manage to solve this? can you help me? I also encounter the same issue. – The Great Mar 18 '22 at 16:13
  • No solution yet @TheGreat. I made a comearound, reading the entire excel file, adding the new data and than overwritting the excel file. – Victor Faraon Mar 21 '22 at 18:36
  • I have an issue with excel file reading and writing using python - Would you be able to help? https://stackoverflow.com/questions/71557677/xlwings-enter-data-in-a-excel-cell-only-when-it-matches-with-df-row-and-col – The Great Mar 22 '22 at 05:14
  • any luck for this issue? – Harsha Biyani Jun 22 '22 at 09:49
  • see my answer, this is on purpose, you have to use startrow to insert below the existing data – jmd Feb 22 '23 at 22:41

5 Answers5

8

I know this is not typical, and most likely will be fixed in future versions of Pandas, but using the startrow with version 1.4.2 worked for me. Try the following code:

writer = pd.ExcelWriter(excelfilepath, engine='openpyxl', mode='a', if_sheet_exists='overlay')

df1.to_excel(writer, sheet_name='Núcleo de TRIAGEM', startrow=writer.sheets['Núcleo de TRIAGEM'].max_row, header=None)

df2.to_excel(writer, sheet_name='Núcleo de FALÊNCIAS', startrow=writer.sheets['Núcleo de FALÊNCIAS'].max_row, header=None)

df3.to_excel(writer, sheet_name='RE - Triagem', startrow=writer.sheets['RE - Triagem'].max_row, header=None)

df4.to_excel(writer, sheet_name='RE - Falências', startrow=writer.sheets['RE - Falências'].max_row, header=None)

writer.save()
Omar Zaki
  • 191
  • 6
  • Tried this but got "Exception has occurred: IndexError At least one sheet must be visible" – MrT77 Dec 06 '22 at 10:03
  • This exception about visible sheet is risen when you try to close the file without actually writing data to it. You have something wrong in your code, maybe an exception before you get to the end. – jmd Feb 22 '23 at 22:43
2

For anyone in the future. Check your Pandas version if if_sheet_exists='overlay' is not working. That was added to pandas in the version 1.4.0. Just try updating it.

Carlost
  • 478
  • 5
  • 13
1

Nothing is missing. The intent of the overlay option is to write over the existing data, and not to append the new data below it.
The old data will in fact remain only for the part that was larger than the new one you're writing, or if you are explicitly setting startrow to a value high enough to write your new data below it.

As proposed already, you can do it like this :

df1.to_excel(writer, sheet_name='sheet1', startrow=writer.sheets['sheet1'].max_row, header=None)

This all becomes more obvious when you see that the initial name proposed for that option was overwrite_cells and not overlay.

jmd
  • 877
  • 8
  • 12
0

I was able to fix this by upgrading my version of pandas.

This requires you to first verify your version of pandas. You can do this by

print(pandas.__version__)

In my case, I was using pandas 1.3.5. I then ran

pip install --upgrade pandas

It then uninstalled the old version and installed the new version. Also, this did not work when I ran it as sudo or admin user.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
oldstudent
  • 25
  • 6
0

We can write to excel by creating a utility function.

def save_excel_sheet(df, excel_path, sheet_name):
    if not os.path.exists(excel_path):
        df.to_excel(excel_path, sheet_name=sheet_name, index=False)
    else:
        with pd.ExcelWriter(excel_path, engine='openpyxl', if_sheet_exists='overlay', mode='a') as writer:
            df.to_excel(writer, sheet_name=sheet_name, startrow=writer.sheets[sheet_name].max_row, header=None, index=False)

and call the function like below

save_excel_sheet(df1, excel_path, sheet_name='Núcleo de TRIAGEM')

save_excel_sheet(df3, excel_path, sheet_name='RE - Triagem')

This will create a file if not exists and append to the provided sheet if the file exists.

Pandas Version - 2.0.2

Lijo Abraham
  • 841
  • 8
  • 30