Open In App

How to Write Pandas DataFrames to Multiple Excel Sheets?

Last Updated : 18 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Writing multiple Pandas DataFrames to different sheets in an Excel file is a common task when organizing structured data. For example, if you have separate datasets like sales reports, inventory logs and customer feedback, you might want each to appear on a different sheet within a single Excel file (e.g., Sales, Inventory, Feedback). Let’s explore different methods to perform this efficiently using Python.

Using ExcelWriter

This is the standard and simplest way to write multiple DataFrames to different sheets in a single Excel file. You open an ExcelWriter, write each DataFrame with a specific sheet name, and save. Ideal for generating organized multi-sheet reports.

Example 1: In this example, we create three separate DataFrames, one for fruits, one for vegetables and one for baked items and write each of them to a different sheet in the same Excel file.

Python
import pandas as pd

df1 = pd.DataFrame({
    'Fruits': ['Apple', 'Banana', 'Mango', 'Dragon Fruit', 'Musk Melon', 'Grapes'],
    'Sales (kg)': [20, 30, 15, 10, 50, 40]
})

df2 = pd.DataFrame({
    'Vegetables': ['Tomato', 'Onion', 'Ladies Finger', 'Beans', 'Beetroot', 'Carrot'],
    'Sales (kg)': [200, 310, 115, 110, 55, 45]
})

df3 = pd.DataFrame({
    'Baked Items': ['Cakes', 'Biscuits', 'Muffins', 'Rusk', 'Puffs', 'Cupcakes'],
    'Sales (kg)': [120, 130, 159, 310, 150, 140]
})

with pd.ExcelWriter("sales_report.xlsx") as writer:
    df1.to_excel(writer, sheet_name="Fruits", index=False)
    df2.to_excel(writer, sheet_name="Vegetables", index=False)
    df3.to_excel(writer, sheet_name="Baked Items", index=False)

Output

Output
Excel_Output
Multi-Sheet (sales_report.xlsx file)

Explanation: pd.ExcelWriter() as a context manager open a writer object. Then, we write each DataFrame to a specific sheet using .to_excel(), specifying a unique sheet_name. Setting index=False excludes the DataFrame index column from being written to the Excel file.

Example 2: In this example, we create a simple DataFrame containing product data and append it as a new sheet to an existing Excel file without modifying the current sheets.

Python
import pandas as pd

df = pd.DataFrame({
    'Product': ['Marker', 'Board'],
    'Qty': [10, 5]
})

with pd.ExcelWriter('existing_file.xlsx', mode='a', engine='openpyxl', if_sheet_exists='new') as writer:
    df.to_excel(writer, sheet_name='NewSheet', index=False)

Output

Initial_file
Initial Sheet (existing_file.xlsx)
Output
Appended Sheet (existing_file.xlsx)

Explanation: We use mode='a' to append and engine='openpyxl' to ensure compatibility with .xlsx files. The if_sheet_exists='new' ensures a new sheet is added instead of overwriting an existing one.

Using openyxl

openpyxl engine is specifically designed to work with .xlsx Excel files. When combined with ExcelWriter, it allows you to modify existing Excel files such as adding new sheets, updating data or preserving existing formatting and formulas. It's essential when you need to maintain the structure of an Excel file while programmatically adding or changing content.

Initial_file
Initial Sheet(existing.xlsx)
Python
import pandas as pd
df = pd.DataFrame({'Drinks': ['Coke', 'Pepsi'], 'Qty': [50, 60]})

with pd.ExcelWriter('existing.xlsx', engine='openpyxl', mode='a') as writer:
    df.to_excel(writer, sheet_name='CoolDrinks')

Output

Output
CoolDrinks Sheet (existing.xlsx)

Explanation: This example shows how to open an existing .xlsx file and append a new sheet named CoolDrinks without affecting existing content.

Using ZipFile Module

This method helps save Excel files inside a .zip archive using Python’s built-in zipfile module. It’s particularly useful when dealing with large files. You create the Excel content in memory using a buffer and then compress it directly saving space and making file transfers faster.

Python
import pandas as pd
import zipfile

df1 = pd.DataFrame({'Fruits': ['Apple', 'Banana'], 'Qty': [10, 15]})
df2 = pd.DataFrame({'Drinks': ['Coke', 'Pepsi'], 'Qty': [25, 30]})

with zipfile.ZipFile("compressed_output.zip", "w") as zf:
    with zf.open("report.xlsx", "w") as buffer:
        with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer:
            df1.to_excel(writer, sheet_name='Fruits', index=False)
            df2.to_excel(writer, sheet_name='Drinks', index=False)

Output

Output
Zipped Excel
Output
report.xlsx File

Explanation: Here, we use zipfile.ZipFile to create a zip archive and write an Excel file directly into it. xlsxwriter is used to generate the Excel file in memory and buffer serves as an in-memory stream for writing.


Next Article
Practice Tags :

Similar Reads