xlcalculator is a Python library that reads MS Excel files and, to the extent of supported functions, can translate the Excel functions into Python code and subsequently evaluate the generated Python code. Essentially doing the Excel calculations without the need for Excel.
xlcalculator is a modernization of the koala2 library.
xlcalculator currently supports:
Loading an Excel file into a Python compatible state
Saving Python compatible state
Loading Python compatible state
Extracting sub-portions of a model. “focussing” on provided cell addresses or defined names
Defined Names (a “named cell” or range)
Shared formulas not an Array Formula
- Operands (+, -, /, *, ==, <>, <=, >=)
- on cells only
Set cell value
Get cell value
- Code is in examples\third_party_datastructure
Functions are at the bottom of this README
- Python Math.log() differs from Excel LN. Currently returning Math.log()
- VLOOKUP – Exact match only
- YEARFRAC – Basis 1, Actual/actual, is only within 3 decimal places
Not currently supported:
Array Formulas or CSE Formulas (not a shared formula): https://stackoverflow.com/questions/1256359/what-is-the-difference-between-a-shared-formula-and-an-array-formula or https://support.office.com/en-us/article/guidelines-and-examples-of-array-formulas-7d94a64e-3ff3-4686-9372-ecfd5caa57c7)
- Functions required to complete testing as per Microsoft Office Help website for SQRT and LN
Setup your environment:
virtualenv -p 3.7 ve ve/bin/pip install -e .[test]
From the root xlcalculator directory:
ve/bin/py.test -rw -s --tb=native
Or simply use
From the examples/common_use_case directory:
Adding/Registering Excel Functions
Excel function support can be easily added.
Fundamental function support is found in the xlfunctions directory. The functions are thematically organised in modules.
Excel functions can be added by any code using the
xlfunctions.xl.register() decorator. Here is a simple example:
from xlcalculator.xlfunctions import xl @xl.register() @xl.validate_args def ADDONE(num: xl.Number): return num + 1
The @xl.validate_args decorator will ensure that the annotated arguments are converted and validated. For example, even if you pass in a string, it is converted to a number (in typical Excel fashion):
>>> ADDONE(1): 2 >>> ADDONE('1'): 2
If you would like to contribute functions, please create a pull request. All new functions should be accompanied by sufficient tests to cover the functionality. Tests need to be written for both the Python implementation of the function (tests/xlfunctions) and a comparison with Excel (tests/xlfunctions_vs_excel).
Excel number precision
Excel number precision is a complex discussion.
It has been discussed in a Wikipedia page.
The fundamentals come down to floating point numbers and a contention between how they are represented in memory Vs how they are stored on disk Vs how they are presented on screen. A Microsoft article explains the contention.
This project is attempting to take care while reading numbers from the Excel file to try and remove a variety of representation errors.
Further work will be required to keep numbers in-line with Excel throughout different transformations.
From what I can determine this requires a low-level implementation of a numeric datatype (C or C++, Cython??) to replicate its behaviour. Python built-in numeric types don’t replicate behaviours appropriately.
Unit testing Excel formulas directly from the workbook.
Do not treat ranges as a granular AST node it instead as an operation “:” of two cell references to create the range. That will make implementing features like
A1:OFFSET(...)easy to implement.
Support for alternative range evaluation: by ref (pointer), by expr (lazy eval) and current eval mode.
- Pointers would allow easy implementations of functions like OFFSET().
- Lazy evals will allow efficient implementation of IF() since execution of true and false expressions can be delayed until it is decided which expression is needed.
Implement array functions. It is really not that hard once a proper RangeData class has been implemented on which one can easily act with scalar functions.
Refactor model and evaluator to use pass-by-object-reference for values of cells which then get “used”/referenced by ranges, defined names and formulas
Handle multi-file addresses
Improve integration with pyopenxl for reading and writing files example of problem space
Function xlcalculator PyCel formulas Koala FLOOR
Date and Time
Function xlcalculator PyCel formulas Koala DATE
Function xlcalculator PyCel formulas Koala BIN2DEC
Function xlcalculator PyCel formulas Koala IRR
Function xlcalculator PyCel formulas Koala ISBLANK
Function xlcalculator PyCel formulas Koala AND
Lookup and reference
Function xlcalculator PyCel formulas Koala CHOOSE
Math and Trigonometry
Function xlcalculator PyCel formulas Koala ABS
Function xlcalculator PyCel formulas Koala AVERAGE
Function xlcalculator PyCel formulas Koala CONCAT