0

I am trying to write my dataframe to excel. I am able to write the data using pandas.

df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', index = False)

But the excel I am trying to write contain some template which look something like this. enter image description here

Whenever I try to write the df values to excel using df.to_excel it always remove the template and write is there way I can write the data below the template in excel.

Any suggestions?

rohi
  • 175
  • 10
  • try with append mode: `mode='a'` – Harsha Biyani Oct 27 '21 at 05:32
  • Does this answer your question? [How to write to an existing excel file without overwriting data (using pandas)?](https://stackoverflow.com/questions/20219254/how-to-write-to-an-existing-excel-file-without-overwriting-data-using-pandas) – Binit Amin Nov 01 '21 at 14:20

1 Answers1

1

I am able to solve this using below code:

import pandas as pd
from openpyxl import load_workbook

path = "Excel.xlsx"
book = load_workbook(path)
writer = pd.ExcelWriter("Excel.xlsx", engine='openpyxl')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
df.to_excel(writer, startrow=writer.sheets['Sheet1'].max_row, index=False, header=False)
writer.save()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
rohi
  • 175
  • 10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '21 at 07:41