(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to Export a DataFrame to Excel with pandas in Python

João Barros 07 de July de 2026 4 min read

Exporting a DataFrame to Excel with pandas is one of the fastest ways to share analysis results with colleagues who live in Excel. Instead of handing over raw data, you deliver a clean .xlsx file with the right columns, ready to open. This guide shows how to do it, from the simplest case to writing several sheets into the same file.

Prerequisites

  • Python 3.9 or newer installed.
  • The pandas and openpyxl libraries (openpyxl is the engine that writes .xlsx files).
  • A DataFrame with data. In this example we build one from a dictionary, but any source works: CSV, database or API.

Step 1: Install pandas and openpyxl

Before exporting to Excel, make sure both libraries are installed. pandas handles the data and openpyxl writes the .xlsx format that Excel opens. If you use virtual environments, activate yours before installing.

pip install pandas openpyxl

Step 2: Create or load the DataFrame

For this example we use a small sales DataFrame. In practice this data may come from a CSV file, a SQL query or an API — the export step is always the same, whatever the source of the data.

import pandas as pd

dados = {
    "Produto": ["Teclado", "Rato", "Monitor"],
    "Unidades": [12, 30, 8],
    "Preco": [19.90, 9.50, 149.00],
}
df = pd.DataFrame(dados)
print(df)

Step 3: Export the DataFrame to Excel

The basic export uses the to_excel method. The index=False argument avoids writing the pandas index column, which is rarely useful in the final file and often confuses whoever opens the sheet.

df.to_excel("vendas.xlsx", index=False)

This creates a vendas.xlsx file in the same folder as the script, with a sheet named "Sheet1". To name the sheet yourself, use the sheet_name argument:

df.to_excel("vendas.xlsx", sheet_name="Vendas", index=False)
If you see the error "No module named 'openpyxl'", install the engine with pip install openpyxl. That is what pandas uses to write .xlsx files.

Step 4: Write several DataFrames to different sheets

Each call to to_excel with a file name creates a brand-new workbook and overwrites the previous one. So, to combine more than one table in the same file — for example a detail view and a summary — we use ExcelWriter as a context manager (a with block). Each DataFrame is written to its own sheet.

resumo = df.groupby("Produto")["Unidades"].sum().reset_index()

with pd.ExcelWriter("relatorio.xlsx", engine="openpyxl") as writer:
    df.to_excel(writer, sheet_name="Detalhe", index=False)
    resumo.to_excel(writer, sheet_name="Resumo", index=False)

The with block makes sure the file is written and closed correctly at the end, without you having to call any save method manually.

Step 5: Pick columns and handle missing values

The to_excel method accepts handy arguments to fine-tune the output: columns selects and orders the columns to export, na_rep replaces missing values with text of your choice, and startrow leaves blank rows at the top (for a title, for instance).

df.to_excel(
    "vendas.xlsx",
    sheet_name="Vendas",
    index=False,
    columns=["Produto", "Unidades"],
    na_rep="s/ dados",
)

These arguments let you match an existing report layout without opening Excel or editing anything by hand afterwards.

Verify the result

Open the generated file in Excel and confirm the data appears in the right sheets and without the index column. If you prefer to validate in code, read the file back with pandas and compare it with the original:

verificacao = pd.read_excel("vendas.xlsx", sheet_name="Vendas")
print(verificacao.head())

If the result shows the same rows you exported, the export worked.

Conclusion

With to_excel and ExcelWriter you go from data in Python to Excel reports in just a few lines, which is ideal for sharing results with people who do not code. As next steps, try automating a weekly report or applying advanced formatting — column widths and colours — directly with openpyxl. Which repetitive task on your team could you remove by writing the result straight to Excel?