Registry / serialization / formulas

formulas

JSON →
library1.3.4pypypi✓ verified 91d ago

Formulas is a Python library that implements an interpreter for Excel formulas, parsing and compiling Excel formula expressions. It can also compile Excel workbooks to Python code and execute them without needing a Microsoft Excel COM server. The current version is 1.3.4, released on March 11, 2026, with an active, though irregular, release cadence.

pip install formulas
INSTALL
IMPORT
SIG · FORMULAS
F
formulas
serializationpythonv1.3.4
Install
10.6s avg
Import
470ms
Disk
285MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.3.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.940 runs
installs and imports cleanly · install 0.0s · import 0.475s · 283.6MB
glibc
py 3.10–3.940 runs
installs and imports cleanly · install 10.6s · import 0.464s · 275MB
285MB installed
● package 285MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
✓ from formulas import Parser
ExcelModel
✓ from formulas import ExcelModel
✗ from formulas.excel import ExcelModel
ExcelModel is directly available from the top-level 'formulas' package after installation of the 'excel' extra.
get_functions
✓ import formulas functions = formulas.get_functions()

This quickstart demonstrates how to parse and compile a basic Excel formula using the `Parser` class and how to extend the interpreter with custom Python functions. It showcases two core capabilities of the library: formula evaluation and customization.

import formulas # 1. Parse and compile a simple Excel formula parser = formulas.Parser() ast_node = parser.ast("=SUM(10, 20)")[1] # [1] gets the formula node compiled_func = ast_node.compile() result_sum = compiled_func() # Expected: 30 print(f"Result of '=SUM(10, 20)': {result_sum}") # 2. Add a custom function FUNCTIONS = formulas.get_functions() FUNCTIONS['MYCUSTOMADD'] = lambda x, y: x + y + 100 custom_func_node = parser.ast("=MYCUSTOMADD(5, 7)")[1] compiled_custom_func = custom_func_node.compile() result_custom = compiled_custom_func() # Expected: 5 + 7 + 100 = 112 print(f"Result of '=MYCUSTOMADD(5, 7)': {result_custom}")
Debug
Known issues
gotchaTo work with Excel workbooks (e.g., loading, calculating, writing Excel files via `ExcelModel`), you must install the `formulas` library with the `excel` extra. Use `pip install formulas[excel]` or `pip install formulas[all]`.
fix
Ensure installation with `pip install formulas[excel]` or `pip install formulas[all]` if `ExcelModel` is used.
affects: All versions
gotchaThe library's interpretation of complex Excel formulas, especially those with empty parameters or nuanced operator precedence, has evolved. Formulas that rely on edge-case behaviors might produce different results across minor versions due to internal parser corrections.
fix
Thoroughly test existing complex formulas after upgrading the `formulas` library to ensure consistent behavior. Refer to the project's changelog for specific parser adjustments.
affects: All versions, particularly when upgrading from older 1.x releases.
gotchaWhen defining custom functions, they must be explicitly registered with the `formulas.get_functions()` dictionary. Forgetting this step will lead to formula parsing errors when the custom function is referenced.
fix
Always retrieve the global functions dictionary using `formulas.get_functions()` and assign your custom function to a key (its Excel name) within that dictionary before parsing formulas that use it.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'formulas'
The 'formulas' library is not installed in your Python environment or there is a typo in the import statement.
fix
Install the library using pip: `pip install formulas`
FormulaError: ('Not a valid formula:\n%s', "='Sheet 1'!#REF!")
The Excel formula being processed by the 'formulas' library contains an invalid reference, similar to Excel's #REF! error, often caused by deleted cells or incorrect sheet/range names in the formula itself.
fix
Review the Excel file for formula validity, ensuring all cell, range, and sheet references are correct and exist. For example, if 'Sheet 1'!#REF! is the issue, verify 'Sheet 1' and the referenced cell/range within it are valid.
AttributeError: 'ExcelModel' object has no attribute 'calculate'
This error typically occurs when attempting to call the 'calculate()' method on an 'ExcelModel' object before the Excel workbook has been successfully loaded and 'finished' (i.e., parsed and compiled).
fix
Ensure the Excel workbook is correctly loaded and parsed using `formulas.ExcelModel().loads(filename).finish()` before calling `calculate()`.
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This generic Python error often arises when the 'formulas' library processes an Excel formula that implicitly or explicitly attempts an operation (like addition) between incompatible data types (e.g., a number and a text string) during Python's evaluation.
fix
Review the Excel formula and the data types of its inputs. Ensure that all operands involved in arithmetic or other type-sensitive operations are of compatible types. If necessary, adjust the Excel formula or preprocess the input data to ensure type consistency.
Upgrade
Version history
1.3.4latest on PyPI · released Mar 11, 2026
Audit
Dependencies
openpyxloptionalEnables Excel workbook compilation and execution via `formulas[excel]` extra.
matplotliboptionalEnables plotting formula Abstract Syntax Trees (ASTs) and Excel models via `formulas[plot]` extra.
FlaskoptionalEnables the HTTP API server (`formulas serve`) via `formulas[web]` extra.
Agent activity
14 hits · last 30 days
node
14
Resources
formulas — pip install formulas · libregistry