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:
- Install dependencies: Make sure you have pandas and openpyxl installed. You can install them using pip:
pip install pandas openpyxl
- 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
- Load the existing Excel file: Use the
load_workbook
function to load the existing Excel file. Specify theread_only
andkeep_vba
arguments asFalse
to allow editing the file, and setdata_only
toFalse
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')
writer.book = book
- 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)
- 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']})
- 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 thestartrow
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 setstartrow
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)
- Save the changes and close the file: After writing the new data, save the changes to the Excel file and close it:
writer.save()
writer.close()
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.
+ There are no comments
Add yours