Python
Excel
Spreadsheet
Data Manipulation
Python Libraries

How to write to an Excel spreadsheet using Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python can write Excel files through several libraries, but the two most common workflows are: use pandas when your data is already tabular, or use openpyxl when you need cell-level control. The right tool depends on whether you are exporting a table or building a workbook more manually.

Write a DataFrame with pandas

If your data is already in a DataFrame, to_excel is the fastest route:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Cara"],
6        "score": [91, 88, 95],
7    }
8)
9
10df.to_excel("results.xlsx", index=False)

That creates an Excel workbook with one sheet and writes the rows directly. index=False avoids exporting the DataFrame index as an extra column.

Write Multiple Sheets

Pandas can also write several DataFrames into one workbook:

python
1import pandas as pd
2
3students = pd.DataFrame({"name": ["Alice", "Bob"], "score": [91, 88]})
4summary = pd.DataFrame({"metric": ["mean"], "value": [students["score"].mean()]})
5
6with pd.ExcelWriter("report.xlsx") as writer:
7    students.to_excel(writer, sheet_name="students", index=False)
8    summary.to_excel(writer, sheet_name="summary", index=False)

This is a good pattern for reports that need more than one worksheet.

Use openpyxl for Cell-Level Control

If you need to control individual cells, formulas, or workbook structure directly, use openpyxl:

python
1from openpyxl import Workbook
2
3wb = Workbook()
4ws = wb.active
5ws.title = "Results"
6
7ws["A1"] = "Name"
8ws["B1"] = "Score"
9
10ws["A2"] = "Alice"
11ws["B2"] = 91
12ws["A3"] = "Bob"
13ws["B3"] = 88
14
15wb.save("results_openpyxl.xlsx")

This approach is more verbose than pandas, but it gives you direct workbook editing behavior.

Add Simple Formatting

openpyxl also lets you style cells:

python
1from openpyxl import Workbook
2from openpyxl.styles import Font
3
4wb = Workbook()
5ws = wb.active
6
7ws["A1"] = "Name"
8ws["B1"] = "Score"
9
10bold = Font(bold=True)
11ws["A1"].font = bold
12ws["B1"].font = bold
13
14wb.save("formatted.xlsx")

That is often enough for headers, emphasis, or basic report polish.

Choose the Library by Use Case

A practical rule is:

  • use pandas for DataFrame exports
  • use openpyxl for workbook editing and formatting
  • use both together when needed, because pandas can write the data and openpyxl can refine the workbook afterward

This split keeps the code simpler than forcing one tool to do everything.

File Format Notes

Most modern Python Excel workflows target .xlsx. Older .xls support is much more limited and is rarely the best default for new code.

Also remember that Excel files are not plain text. If the file is open in Excel while your script writes it, the save can fail depending on the operating system and application state.

Updating an Existing Workbook

If you need to edit an existing Excel file instead of creating a new one, openpyxl is usually the better tool:

python
1from openpyxl import load_workbook
2
3wb = load_workbook("results.xlsx")
4ws = wb["Results"]
5ws["C2"] = "Passed"
6wb.save("results.xlsx")

That kind of in-place workbook editing is much harder to express cleanly with a pure pandas export workflow.

Common Pitfalls

The biggest pitfall is forgetting index=False in pandas and accidentally exporting the DataFrame index as a normal column.

Another common issue is mixing up libraries. openpyxl works on .xlsx files and provides workbook control, while pandas is mainly about structured table export.

People also forget that writing to an existing workbook may overwrite sheets or files if they do not choose append or update behavior deliberately.

Summary

  • Use DataFrame.to_excel() when your data is already in pandas.
  • Use openpyxl when you need cell-by-cell control or formatting.
  • Write multiple sheets with pd.ExcelWriter.
  • Prefer .xlsx for modern Excel output.
  • Pick the library based on whether your task is tabular export or workbook manipulation.

Course illustration
Course illustration

All Rights Reserved.