Sub topic 11
Excel engines and file formats
This chapter assumes you can run Python scripts, install packages with pip, and have already used pandas.read_excel to load a sheet. It covers how pandas reads Excel files, which package it uses for each format, and the errors you see when that package is missing.
What an engine is
pandas does not read Excel files itself. It hands the work to a library called an engine. read_excel picks one from the file extension, so you rarely name it. The engine must be installed separately from pandas.
| Extension | Format | Default engine | Install |
|---|---|---|---|
| `.xlsx`, `.xlsm` | Excel 2007+ (zipped XML) | openpyxl | `pip install openpyxl` |
| `.xls` | Excel 97-2003 (binary) | xlrd | `pip install xlrd` |
| `.xlsb` | Excel binary workbook | pyxlsb | `pip install pyxlsb` |
| `.ods` | OpenDocument spreadsheet | odfpy | `pip install odfpy` |
Choosing an engine explicitly
Pass engine= when the extension does not match the real format, or when you want a specific library. This example writes a workbook to memory and reads it back with the engine named.
import pandas as pd
from io import BytesIO
buffer = BytesIO()
pd.DataFrame({"a": [1, 2], "b": ["x", "y"]}).to_excel(
buffer, engine="openpyxl", index=False
)
buffer.seek(0)
df = pd.read_excel(buffer, engine="openpyxl")
print(df)A file-like object has no extension, so pandas cannot guess the format from a name. Naming the engine is the reliable choice there.
Common errors
Most failures come from a missing or mismatched engine.
ImportError: Missing optional dependency 'openpyxl': the engine for that format is not installed. Install it with pip in the same environment that runs your code.ValueError: Excel file format cannot be determined, you must specify an engine manually: the input has no usable extension, as with a buffer or a file saved without one. Passengine=.XLRDError: Excel xlsx file; not supported: this appears with old xlrd versions that were asked to read.xlsx. Current xlrd reads only.xls, so use openpyxl for.xlsx.BadZipFileor a similar error on a file named.xlsx: the file is not a real workbook. Some systems export HTML or CSV text with an.xlsxor.xlsname. Open it in a text editor to check, and useread_htmlorread_csvif that is what it holds.
Checking what is installed
Before a long job, confirm the engine imports in your environment:
import importlib
for name in ["openpyxl", "xlrd", "pyxlsb", "odf"]:
try:
module = importlib.import_module(name)
print(name, "available", getattr(module, "__version__", ""))
except ImportError:
print(name, "missing")The package odfpy is imported as odf, which is why the list uses that name.
Format limits worth knowing
.xlsxfiles can hold formulas.read_excelreturns the last value Excel saved for each formula cell, not the formula text. A file generated by code and never opened in Excel may have no saved values, so those cells load as missing..xlsfiles are limited to 65,536 rows and 256 columns, which is a reason to prefer.xlsxfor new data.- Excel files are slower to load than CSV. For large tables, consider exporting to CSV and using the chunked reading from the earlier chapter.
Exercise
A colleague sends report.xls. You run pd.read_excel("report.xls") and get ImportError: Missing optional dependency 'xlrd'. What do you do, and what would you do differently if the file were report.xlsx?
Show answer
Install xlrd with pip install xlrd in the environment that runs the code, then run the read again. For report.xlsx, install openpyxl instead, because pandas uses openpyxl for .xlsx files and xlrd only reads the older .xls format.
Exercise
You load a workbook from a BytesIO buffer and get "Excel file format cannot be determined, you must specify an engine manually". How do you fix it?
Show answer
Pass the engine explicitly, for example pd.read_excel(buffer, engine="openpyxl"). A buffer has no file extension, so pandas cannot choose an engine on its own.