Skip to main content

xlcalcualtor converts MS Excel formulas to Python and evaluates them.

Project description

Excel Calculator

In short xlcalculator is an attempted re-write of the koala2 library with intent to modernize the code, increase maintainability and keep compatibility with external software solutions (self-indulgently one is - FlyingKoala ).

Koala2's heritage has been "spreadsheet replacement" in order to get performant calculation. In essence replacing the calculation engine in Excel with one which works faster. And, all credit to the civic hacker crew at we are ants, they absolutely succeeded. In various iterations of the project since it appears as though the koala2 has tried moving toward the idea of something like openpyxl with a "calculate" button.

This is not the direction I've taken.

Koala2 had a few features which enabled me to use koala2 quite differently. I admit these features may not have been core to the wider audience. The features I am specifically interested in are the "advanced" features of setting output cells on load and saving/loading the state of the Python representation of the worksheet. With setting the output cell on load it was possible to load only one formula, and/or the related parent formulas and the input cells involved with the network of formulas. In essence focusing the resulting "loaded Excel" to far less than the entire workbook. And, once in Python, it could be persisted (saved) and re-loaded. This created a mathematical model defined in the language of Excel formulas but executable ("evaluated") and iterable in Python without the need for a person to translate it.

This use case provides the ability to view Excel workbooks as definitions of a calculation (a model or part thereof). Where the model is an abstraction that just so happens to be expressed in the form of an Excel spreadsheet. So, in that sense, koala2 wasn't simply replacing the Excel calculation engine but was more a toolkit which uses Excel as an interface for defining parts or whole mathematical systems. I saw that as very useful.

These features I so covet have broken after some (much needed) code clean-up and I've struggled to re-implement them. With that, I have decided to use some parts of koala2 and implement a library which will have the features I'm interested in. I'm happy if this implementation (or something similar) becomes adopted by the koala2 project (I'd prefer to not split a universe) but, equally, I accept my goals may not overlap enough with that project.

Moving forward, this project, if used in one way, would achieve the purposes of replacing the Excel calculation engine or even be analogous to openpyxl with a "go" button. But would also offer significantly more for those who are inextricably bound to Excel but need more from their math. Which is what FlyingKoala is intended to facilitate.

We have some very basic functionality working. That said, the project is still evolving and has some way to go before it can claim to be feature compatible with koala2.

xlcalculator currently supports:

  • Loading an Excel file into a Python compatible state
  • Saving Python compatible state
  • Loading Python compatible state
  • Ignore worksheets
  • Extracting sub-portions of a model. "focussing" on provided cell addresses or defined names
  • Evaluating
    • Individual cells
    • Defined Names (a "named cell" or range)
    • Ranges
    • Shared formulas not an Array Formula
    • Operands (+, -, /, *, ==, <>, <=, >=)
      • on cells only
    • Set cell value
    • Get cell value
    • Parsing a dict into the Model object
      • Code is in examples\third_party_datastructure
    • Functions as implemented in xlfunctions.
      • ABS
      • AVERAGE
      • CHOOSE
      • CONCAT
      • COUNT
      • COUNTA
      • DATE
      • IRR
      • LN
        • Python Math.log() differs from Excel LN. Currently returning Math.log()
      • MAX
      • MID
      • MIN
      • MOD
      • NPV
      • PMT
      • POWER
      • RIGHT
      • ROUND
      • ROUNDDOWN
      • ROUNDUP
      • SLN
      • SQRT
      • SUM
      • SUMPRODUCT
      • TODAY
      • VLOOKUP
        • Exact match only
      • XNPV
      • YEARFRAC
        • Basis 1, Actual/actual, is only within 3 decimal places

Not currently supported:

Run tests

From the root xlcalculator directory

python -m unittest discover -p "*_test.py"

Run Example

From the examples/common_use_case directory

python use_case_01.py

How to add Excel functions

Excel function support can be easily added.

Fundamental function support is supplied by xlfunctions, so to add the "recipe for calculation" please submit a pull request to that project. There are instructions in that project. Please be conscientious with writing tests in that project as they are the tests for how the calculation operates.

It is also best for your submission to have an evaluation test here in xlcalculator so we can ensure that the results of the xlfunction implementation are aligning with what we see in Excel.

Excel number precision

Excel number precision is a complex discussion. There is further detail on the README at xlfunctions.

Unit testing Excel formulas directly from the workbook.

If you are interested in unit testing formulas in your workbook, you can use FlyingKoala. An example on how can be found here.

TODO

  • Set up a travis continuous integration service
  • Improve testing
  • 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 (Maybe integrating xlcalculator with pyopenxl is actually Koala3?) Example of problem space here

BUGS

  • Formatted text in a cell (eg; a subscript) breaks the reader.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xlcalculator-0.0.6b0.tar.gz (48.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xlcalculator-0.0.6b0-py3-none-any.whl (82.2 kB view details)

Uploaded Python 3

File details

Details for the file xlcalculator-0.0.6b0.tar.gz.

File metadata

  • Download URL: xlcalculator-0.0.6b0.tar.gz
  • Upload date:
  • Size: 48.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for xlcalculator-0.0.6b0.tar.gz
Algorithm Hash digest
SHA256 1b8a03bd2b7664f69e22609e5a4cffc1fa0d93705d55b38bae0b521662602cc2
MD5 d0ef0a42324a210df66a3c62d8a73d6b
BLAKE2b-256 2646144f767af8d557600ce7c2bd6ea2daa6ef2cfa0757ed37dd5a2dbd89127d

See more details on using hashes here.

File details

Details for the file xlcalculator-0.0.6b0-py3-none-any.whl.

File metadata

  • Download URL: xlcalculator-0.0.6b0-py3-none-any.whl
  • Upload date:
  • Size: 82.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for xlcalculator-0.0.6b0-py3-none-any.whl
Algorithm Hash digest
SHA256 29e5f7d5f2d6b3666c8559f2f6cd009b8d2706f761c19a34714a7401dc4bc08f
MD5 bb070ec1b8e3e6a5be6ce9c967498eb9
BLAKE2b-256 defd91b0c0a4678b6b64c1823d13ff6dcce40c0017072cba4f97724d3e444217

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page