Migration from openpyxl
Replace openpyxl with CrabXL and learn the differences.
Change the import
crabxl-openpyxl uses the openpyxl 3.1.5 module layout and call names. Install
it with pip install crabxl-openpyxl, change the import and keep the rest of
your code.
# Before
import openpyxl
from openpyxl.styles import Font
# After
import crabxl_openpyxl as openpyxl
from crabxl_openpyxl.styles import Font Each public openpyxl.<module> has a crabxl_openpyxl.<module> with the same classes
and functions.
What is the same
Workbook,load_workbook, worksheets and cells.- Values, dates, formulas, rich text and styles.
- Tables, filters, conditional formatting, data validation, comments, hyperlinks, protection, print settings and defined names.
- Images, charts, chartsheets, pivot tables, external links and VBA projects.
- The normal, read-only and write-only modes.
- Paths and file-like objects for input and output.
CrabXL runs the openpyxl 3.1.5 test suite without changes. The tests of the public API pass.
What is different
- CrabXL preserves the parts of a loaded file that you do not change. openpyxl writes the full file again and drops the parts that it does not know.
- CrabXL keeps the names of VBA, external link and comment parts. It also keeps the calculation chain if you do not change formulas.
- When you change a part that holds content CrabXL does not model, that content is dropped on save, as openpyxl does.
- In write-only mode, set the worksheet views and sheet properties before the
first
append. - To open an
.xlsmor.xltmfile, setkeep_vba=True. - Close a loaded workbook with
close()when you are done. This releases the source file.
What is not supported
- The private modules and attributes of openpyxl, for example
openpyxl.reader.excel.ExcelReader,openpyxl.writer.excel,openpyxl.worksheet._readerandcell._style. The Rust core does this work. - Chart data and pivot table results are not calculated. Formulas are
calculated only when you save with
calculate=True; openpyxl cannot do this. quotePrefixandpivotButtonon row and column dimensions.
Move to the native API
The native crabxl API is not a drop-in replacement. It changes these
behaviors on purpose:
| Task | crabxl_openpyxl (as openpyxl) | crabxl |
|---|---|---|
| Open for editing | load_workbook(path) | Workbook.open(path) |
| Formula and cached result | Two loads, one with data_only=True | rng.formula and rng.value from one load |
| Read a missing cell | ws["B2"].value creates the cell | ws["B2"].value creates nothing |
| Write a formula | ws["A1"] = "=SUM(B:B)" | ws["A1"].value = Formula("SUM(B:B)"); text stays text |
| Style a header | Set cell.font on each cell | ws["A1:D1"].style = header |
| Insert rows | ws.insert_rows(5, 3) moves cells only | ws.rows("5:7").insert() updates every reference |
| Stream a read | load_workbook(path, read_only=True) | Reader.open(path) |
| Stream a write | Workbook(write_only=True) | Writer.create(path) |
| Content CrabXL cannot keep | Dropped on save | The save raises UnsupportedError |
| A new workbook | Has a sheet named “Sheet” | Has no sheets |
| A missing sheet | KeyError | NotFoundError (also a LookupError) |
See the native API documentation.