openpyxl is the standard Python library for XLSX files. It is also slow on large workbooks. A file with a few hundred thousand cells can take seconds to open. A script that runs fine on a test file can stall on real data.
This article shows where the time goes and four changes that make openpyxl code faster. Then it shows how to run the same code on a faster engine.
Where the time goes
An XLSX file is a ZIP archive of XML files. openpyxl does all of its work in Python:
- It parses every XML element of a worksheet in Python.
- It creates one Python
Cellobject for every cell in the sheet. - It keeps every cell, value and style in memory until you close the workbook.
- It writes the XML back one element at a time when you save.
Each step is small. Multiplied by a million cells, each step is slow. The largest costs are opening a file and saving it.
Make openpyxl code faster
Read with read_only=True and values_only=True
If you only read data, open the file in read-only mode. openpyxl then reads
rows as you ask for them and does not keep the whole sheet in memory. Ask for
values instead of Cell objects:
from openpyxl import load_workbook
book = load_workbook("data.xlsx", read_only=True)
sheet = book.active
for row in sheet.iter_rows(values_only=True):
print(row)
book.close()
Close a read-only workbook when you finish. It keeps the file open until then.
Write with write_only=True
To create a large file, use write-only mode. openpyxl writes each row to disk when you append it, so memory stays flat:
from openpyxl import Workbook
book = Workbook(write_only=True)
sheet = book.create_sheet()
for number in range(1_000_000):
sheet.append([number, number * 2])
book.save("large.xlsx")
A write-only sheet only accepts append. You cannot go back and change a cell.
Use append instead of ws.cell() in a loop
sheet.append(row) adds a whole row in one call. Writing the same row with
sheet.cell(row=r, column=c, value=v) makes one call per cell, and each call
does its own checks. Build each row as a list and append it.
Do not read max_row inside a loop
sheet.max_row and sheet.max_column look at every cell each time you read
them. This loop gets slower with every row it adds:
for value in values:
sheet.cell(row=sheet.max_row + 1, column=1, value=value) # slow
Count the rows yourself, or use append:
for value in values:
sheet.append([value])
In the benchmark below, 5,000 rows added this way take 0.84 seconds in
openpyxl. Most of that time is max_row.
Switch the engine
These changes help, but openpyxl still parses and writes XML in Python. CrabXL is an XLSX library with the openpyxl interface and an engine written in Rust. Install it:
pip install crabxl
Then change the import:
import crabxl as openpyxl
The rest of your code stays the same. load_workbook, Workbook,
iter_rows, styles, read_only and write_only work as in openpyxl. CrabXL
passes the unchanged openpyxl 3.1.5 test suite for the public API. openpyxl
itself is not a dependency.
Files still open and save as in openpyxl, so you can switch back at any time.
Benchmark
Each case works on a sheet of 20,000 rows and 10 columns: 200,000 cells. Times are the mean of two runs, in seconds. Lower is better.
| Case | openpyxl 3.1.5 | CrabXL 0.2.1 |
|---|---|---|
| Append 200,000 cells | 0.24 | 0.04 |
Read all values with values_only=True |
0.50 | 0.06 |
Read all Cell objects |
0.43 | 0.49 |
Write 200,000 cells with ws.cell() |
0.41 | 0.51 |
| Set font, fill, border and alignment on every cell | 4.17 | 2.73 |
Add 5,000 rows with max_row + 1 |
0.84 | 0.04 |
| Build the sheet and save it | 2.02 | 0.11 |
| Build, save, open again and read all values | 3.35 | 0.17 |
| Open the file, edit 2,000 cells and save | 3.69 | 0.34 |
The largest gains are in opening and saving files, reading values, and
appending rows. Code that reads or writes one Cell object at a time runs at
about the same speed in both libraries. Most of that time is in Python.
Machine: AMD Ryzen 5 5600G, Windows 11, Python 3.13.
Summary
- Read with
read_only=Trueandvalues_only=True. - Write large files with
write_only=True. - Append whole rows, and keep
max_rowout of loops. - To open, save and read large files faster, change one import to
import crabxl as openpyxl.