How to write to an existing excel file without overwriting data using Python pandas?

Estimated read time 2 min read

To write to an existing Excel file without overwriting data using Python pandas, you can use the openpyxl library, which is a dependency of pandas and provides more control over Excel files. Here’s an example of how you can achieve this:

  1. Install dependencies: Make sure you have pandas and openpyxl installed. You can install them using pip:
pip install pandas openpyxl
  1. Import the required libraries: In your Python script, import the pandas library and the load_workbook function from openpyxl:
import pandas as pd
from openpyxl import load_workbook
  1. Load the existing Excel file: Use the load_workbook function to load the existing Excel file. Specify the read_only and keep_vba arguments as False to allow editing the file, and set data_only to False to retain formulas:
excel_file = 'path/to/your/excel_file.xlsx'
book = load_workbook(excel_file, read_only=False, keep_vba=False, data_only=False)
writer = pd.ExcelWriter(excel_file, engine='openpyxl') = book
  1. Read the existing data (optional): If you want to preserve the existing data in the Excel file, you can read it using pandas before making any changes. For example:
df_existing = pd.read_excel(excel_file)
  1. Prepare the new data: Create a pandas DataFrame with the new data you want to append to the Excel file. For example:
df_new = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})
  1. Write the new data to the Excel file: Append the new data to the existing Excel file using the to_excel method of the pandas DataFrame. Set the startrow argument to the desired row number where the new data should start. For example, to append the new data after the existing data, you can set startrow to the number of rows in the existing data:
start_row = df_existing.shape[0] if 'df_existing' in locals() else 0
df_new.to_excel(writer, index=False, sheet_name='Sheet1', startrow=start_row, header=False)
  1. Save the changes and close the file: After writing the new data, save the changes to the Excel file and close it:

That’s it! The new data will be appended to the existing Excel file without overwriting any previous data. Remember to replace 'path/to/your/excel_file.xlsx' with the actual path to your Excel file, and adjust the sheet name and column names according to your specific scenario.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply