How to convert an Excel spreadsheet to Python without rewriting your formulas
A step-by-step guide to turning an Excel model into a tested Python package: tag inputs and outputs, generate the code, and prove it matches the workbook.
Most "Excel to Python" guides show you how to read a spreadsheet with pandas or openpyxl. That gets you the data. It does not get you the logic: the hundreds of formulas, lookups and intermediate cells that turn inputs into a price, a reserve or a forecast. Rewriting that logic by hand is slow, and every rewrite is a new chance for the Python to drift from the workbook the business trusts.
This guide shows a different approach: generate the Python from the workbook itself, then prove it matches.
What you end up with
From one workbook, Codcel generates a Python package that contains:
- one function per calculation, such as
monthly_payment(input) - typed input and output models built with pydantic
- a pytest suite that checks the generated code against the values in your workbook
The Python package calls Codcel's open-source calculation engine, which is compiled from Rust and loaded through FFI. You write ordinary Python, and the formulas run as native code.
Step 1: Tell Codcel what goes in and what comes out
Your formulas stay exactly as they are. You only mark which cells are inputs and which are outputs, using annotations in any text cell:
| Cell A | Cell B |
|---|---|
*I*B1*InterestRate | 0.05 |
*I*B2*LoanAmount | 250000 |
*I*B3*TermYears | 30 |
*O*B5*MonthlyPayment | =PMT(B1/12, B3*12, -B2) |
*I* marks a required input and *O* marks an output. The names become interest_rate, loan_amount, term_years and monthly_payment in the generated code. There are also *ID* (input with a default) and *IX* (optional input) annotations.
If you'd rather not edit the workbook, Codcel can also auto-detect likely inputs (literal cells referenced by formulas) and outputs (formulas nothing else depends on) and let you accept or reject each suggestion. See Inputs & Outputs in the docs.
Step 2: Generate the Python project
Import the workbook into Codcel Desktop, open Generate Project, and select the Python Calculation Library. The Rust calculation library is always generated too: it is the engine every other language builds on.
Step 3: Build and test
pip3 install .
pytest -sFor every output, the generated tests set the inputs to the values in your workbook, run the generated function, and compare the result with the value Excel calculated. A passing suite is your first evidence that the Python matches the spreadsheet.
The tests cover the one set of inputs saved in the workbook, so add your own cases for the edge cases you care about before going to production. The docs list the known differences from Excel, such as floating-point rounding.
Step 4: Call it from your code
# Default formatting
result = monthly_payment(input)
# With a per-call language override for formatted values
result = monthly_payment_with_language(input, "de")When this approach fits
- The workbook is the source of truth. Analysts keep improving the model in Excel, and you regenerate the code instead of re-porting changes by hand.
- The model is big. Hundreds of interdependent formulas, VLOOKUP and XLOOKUP tables, and dynamic arrays are exactly what's painful to rewrite.
- You need to show your working. Generated tests against the workbook's own values give reviewers and auditors something concrete.
If you only need a few numbers out of a sheet, pandas is fine. If you need the calculation, generate it.
Other ways to use it from Python
- A web client. Codcel can also generate a REST server (for example in Rust) plus a typed Python client for it.
- Excel itself. An xlwings integration lets users keep working in Excel while the generated Python runs the calculations.
See Excel to Python for the full list of what's generated, or download Codcel and try it on your own workbook.
Keep reading
Moving an insurance pricing model out of Excel: a production checklist
Rating engines often start life in Excel. Here is a practical checklist for turning one into a governed, tested pricing service without losing what the actuaries built.
One billion rows from an Excel model: the 1BRC with generated Rust
We pointed Codcel at the One Billion Row Challenge: a 12 GB CSV, an ordinary Excel workbook, and a generated Rust REST server. Here's how it works and what it measured.
VLOOKUP, XLOOKUP and INDEX/MATCH: what happens to lookups when Excel becomes code
Lookups are the backbone of most business spreadsheets. Here's how Codcel carries them into generated code, from small in-sheet tables to billions of rows.