openpyxl reads, writes and edits XLSX files in pure Python. Several other libraries do part of that job faster. This article compares them and shows which one fits which task.
The libraries
| Library | Read | Write | Edit a file | openpyxl code works |
|---|---|---|---|---|
| openpyxl | Yes | Yes | Yes | Yes |
| XlsxWriter | No | Yes | No | No |
| python-calamine | Values only | No | No | No |
| CrabXL | Yes | Yes | Yes | Yes |
pandas and polars are not separate engines. They call one of these libraries:
pandas.read_exceluses openpyxl by default. Passengine="calamine"to use python-calamine.DataFrame.to_exceluses XlsxWriter when it is installed, and openpyxl otherwise.polars.read_exceluses calamine by default.write_exceluses XlsxWriter.
openpyxl
openpyxl is the most widely used library. It reads, writes and edits files,
including styles, merged cells, charts and data validation. It is written in
pure Python, so it installs on any platform. It is also the slowest library
here. It keeps a whole workbook in memory unless you use read_only or
write_only mode.
When openpyxl opens a file and saves it again, it drops content it does not read. For example, it drops images and charts in the original file.
XlsxWriter
XlsxWriter only creates new files. It supports charts, images, conditional formatting, tables and most other Excel features. It cannot read a file or change an existing one.
import xlsxwriter
book = xlsxwriter.Workbook("report.xlsx", {"constant_memory": True})
sheet = book.add_worksheet()
sheet.write_row(0, 0, ["Name", "Amount"])
book.close()
python-calamine
python-calamine reads cell values with a Rust engine. It also reads XLS, XLSB and ODS files. It does not read styles, and it cannot write.
from python_calamine import CalamineWorkbook
book = CalamineWorkbook.from_path("data.xlsx")
for row in book.get_sheet_by_index(0).iter_rows():
print(row)
CrabXL
CrabXL reads, writes and edits XLSX files with a Rust engine and the openpyxl interface. Existing openpyxl code runs after one change:
import crabxl as openpyxl
CrabXL supports the public openpyxl 3.1.5 API, including read_only and
write_only modes. When it saves an edited file, it copies the parts you did
not change as they are. It can also calculate formulas before it saves, which
openpyxl cannot do. It reads XLSX and XLSM files only.
Speed and memory
Each test uses a sheet of 100,000 rows and 10 columns: one million cells of numbers and text. Times are the mean of two runs. Memory is the peak memory of the whole Python process. An empty Python process uses 19 MB.
| Task | Library | Time | Peak memory |
|---|---|---|---|
| Write a new file | openpyxl, write_only=True |
7.0 s | 44 MB |
XlsxWriter, constant_memory |
5.1 s | 41 MB | |
CrabXL, write_only=True |
0.9 s | 43 MB | |
| Read all values | openpyxl, read_only=True |
4.6 s | 61 MB |
| python-calamine | 0.6 s | 95 MB | |
CrabXL, read_only=True |
0.4 s | 63 MB | |
| Read into a DataFrame | pandas.read_excel, openpyxl |
6.5 s | 155 MB |
pandas.read_excel, calamine |
1.9 s | 225 MB | |
| Open, change one cell, save | openpyxl | 15.2 s | 533 MB |
| CrabXL | 1.0 s | 49 MB |
openpyxl ran with lxml installed, which makes it write faster. Versions: openpyxl 3.1.5, XlsxWriter 3.3.1, python-calamine 0.9.1, pandas 3.1.1, CrabXL 0.2.1. Machine: AMD Ryzen 5 5600G, Windows 11, Python 3.13.
Which one to use
- To only read values, or to read XLS, XLSB or ODS files, use python-calamine.
- To only create new files with charts and rich formatting, use XlsxWriter.
- To load data into a DataFrame, use pandas with
engine="calamine". - To read, write and edit files with openpyxl code, use CrabXL.
- To run on a platform without prebuilt wheels, use openpyxl.