Coding

pip install pandas openpyxl: Read and Write Excel Files

To read and write Excel files with pandas, run python -m pip install pandas openpyxl. pandas does the table work, and openpyxl is the library it uses underneath for .xlsx files.

The install command

python -m pip install pandas openpyxl

On Windows, py -m pip install pandas openpyxl does the same. The python -m pip form makes sure pip belongs to the interpreter you will run your script with, which avoids the most common "installed but not found" problem described below.

The pandas installation page offers a shortcut that installs pandas plus its Excel dependencies in one go:

python -m pip install "pandas[excel]"

The quotes matter in some shells, because square brackets can be treated as a pattern. The excel extra installs more than openpyxl. According to the pandas page, it covers xlrd, xlsxwriter, openpyxl, pyxlsb, python-calamine and odfpy. If you only work with .xlsx files, pandas openpyxl is the smaller install.

Check that it worked

Save this as excel_test.py and run python excel_test.py:

import pandas as pd

frame = pd.DataFrame({"name": ["Ana", "Ben"], "score": [90, 85]})
frame.to_excel("scores.xlsx", index=False)
print(pd.read_excel("scores.xlsx"))

Example output:

  name  score
0  Ana     90
1  Ben     85

A scores.xlsx file now exists in the current folder. Two defaults from the pandas documentation are visible here. to_excel writes the row index as a column unless you pass index=False, and it names the sheet Sheet1 unless you set sheet_name. read_excel reads the first sheet by default (sheet_name=0).

Writing several sheets to one file

To put more than one DataFrame in a single workbook, use pd.ExcelWriter as a context manager and pass the writer to to_excel for each sheet:

with pd.ExcelWriter("report.xlsx", engine="openpyxl") as writer:
    frame.to_excel(writer, sheet_name="Scores", index=False)
    frame.to_excel(writer, sheet_name="Backup", index=False)

The file is saved when the with block ends, so you do not call a save method yourself. To read a specific sheet back, pass its name: pd.read_excel("report.xlsx", sheet_name="Backup"). Passing sheet_name=None returns every sheet as a dictionary of DataFrames, keyed by sheet name.

Which library does what

PackageRole according to pandas
openpyxlReading and writing Excel 2010 xlsx/xlsm/xltx/xltm files
xlsxwriterWriting xlsx files
xlrdReading xls files (marked deprecated on the page)
python-calamineReading xls/xlsx/xlsm/xlsb/xla/xlam/ods files

The read_excel documentation describes how pandas picks a reader when you do not set engine: OpenDocument files use odf, xls files use xlrd, xlsb files use pyxlsb, and anything else uses openpyxl. So .xlsx files go to openpyxl.

Reading old .xls files

openpyxl does not read the old .xls format. For those files, install xlrd as well:

python -m pip install pandas xlrd

The pandas installation table marks xlrd as the reader for xls files, and notes that it is deprecated in favor of python-calamine. If you can, re-save the file as .xlsx in Excel and use openpyxl instead.

Choosing the engine yourself

You can name the engine in the call:

df = pd.read_excel("scores.xlsx", engine="openpyxl")
frame.to_excel("scores.xlsx", engine="openpyxl", index=False)

For reading, this is worth doing for a specific reason. The read_excel documentation says that since pandas 3.1.0 it is deprecated to rely on openpyxl as the default for xlsx and xlsm files, because the default will change to calamine in a future version. Passing engine="openpyxl" keeps today's behavior. For writing, to_excel accepts "openpyxl" or "xlsxwriter".

Using openpyxl directly

Once it is installed, you can also use openpyxl without pandas. This is useful for cell-level work such as formulas:

from openpyxl import load_workbook

workbook = load_workbook(filename="scores.xlsx")

The openpyxl documentation says data_only decides whether formula cells return the formula (the default) or the value stored the last time Excel read the sheet. It also warns that Workbook.save() overwrites existing files without warning, so write to a new filename when in doubt.

Versions and Python requirements

As of November 2026, PyPI lists pandas 3.1.1 (released November 4, 2026), which requires Python 3.11 or later. It lists openpyxl 3.1.5 (released June 28, 2024), which requires Python 3.8 or later. If pip cannot find a matching pandas version, check python --version first. To see what you have installed:

python -m pip show pandas openpyxl

To make the install repeatable, put both packages in a requirements.txt. The file format is covered in pip install requirements.txt: How It Works and How to Fix Errors.

If it still fails

  • ModuleNotFoundError: No module named 'pandas' or 'openpyxl'. The Python running your script is not the one pip installed into. Install and run with the same interpreter, using python -m pip. Inside a virtual environment, activate it before both steps.
  • An error naming a missing Excel library when you call read_excel or to_excel. pandas is installed but the Excel library is not. Install openpyxl (or xlrd for .xls) into the same environment.
  • error: externally-managed-environment. Your system Python blocks global installs. Use a virtual environment; see Fix pip "error: externally-managed-environment".
  • A file named pandas.py or openpyxl.py in your project. Python searches the script's folder first, so such a file shadows the real package. Rename it.

Frequently asked questions

Do I need openpyxl to use pandas?
Only for Excel files. pandas itself installs with pip install pandas. Reading and writing .xlsx files goes through a separate library, and openpyxl is the one pandas uses by default for .xlsx when you read a file.
What is the difference between openpyxl and xlsxwriter?
Both write .xlsx files. The pandas installation page lists openpyxl for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files, and xlsxwriter for writing xlsx files only. For reading .xlsx with pandas you need openpyxl (or another reader such as python-calamine).
Can pandas read .xls files with openpyxl?
No. The read_excel documentation says xlrd supports old-style Excel files (.xls), and that pandas uses xlrd when the file is in xls format. openpyxl handles the newer formats.
Why does read_excel show a warning about the default engine changing?
The read_excel documentation says that, since pandas 3.1.0, the default engine for xlsx and xlsm files is deprecated and will change from openpyxl to calamine in a future version. Pass engine="openpyxl" explicitly to keep the current behavior.

Sources