Append Data in Excel by Pandas ExcelWriter / to_excel with 2 Examples

How to append new data into existing Excel file. The answer is using Pandas ExcelWriter object.

Appending data to an existing file by Pandas to_excel

As we have seen in the Pandas to_excel tutorial, every time we execute the to_excel method for saving data into the Excel file – if the file does not exist, it creates a new file and saves the data.

However, if a file exists, it overwrites the contents.

For example, consider this program:

If employees.xlsx already exists, it will overwrite and save this data frame.

How to append new data into an existing Excel file

The answer is using Pandas ExcelWriter object.

Consider, we have already created “Employee.xlsx” file.

It has five rows of employees’ data – as shown below:


Now we want to add two more employees’ information without losing the existing data.

The example below shows how with output:



How did it work?

  • In the ExcelWriter, we used append mode i.e. mode=”a”
  • if_sheet_exists parameter is important. We used if_sheet_exists=”overlay” which means writing content to an existing sheet without removing old contents.
  • In the to_excel method, we specified startrow=6, as we already had a record till row number 5 (See first graphic above).
  • The header is kept False because we already have a header in place.

A little about if_sheet_exists

The if_sheet_exists has the following possible values:

  • error – Raises a value error.
  • New – create a new sheet and save the data frame there.
  • Replace – Delete the contents before writing to it.
  • Overlay – already mentioned.

What if Workbook has multiple sheets?

Above example works if you have a single sheet and data is supposed to write there.

In the case, you have multiple sheets (as shown below), how to append data to the specified sheet?

The answer is using “sheet_name” parameter of to_excel.

Suppose, we have the following Workbook with three sheets:


You can see, our Employees sheet is the second number and is not active. Instead, the “Products” sheet is active.

The program below appends to the “Employees” sheet: