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: https://stackoverflow.com/questions/1256359/what-is-the-difference-between-a-shared-formula-and-an-array-formula)
    • Operands (+, -, /, *, ==, <>, <=, >=)
      • on cells only
    • Set cell value
    • Get cell value
    • Functions as implemented in (xlfunctions)[https://github.com/bradbase/xlfunctions].
      • 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)[https://github.com/bradbase/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)[https://github.com/bradbase/xlfunctions].

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

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.4b0.tar.gz (47.9 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.4b0-py3-none-any.whl (85.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xlcalculator-0.0.4b0.tar.gz
  • Upload date:
  • Size: 47.9 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.4b0.tar.gz
Algorithm Hash digest
SHA256 9a8f0c581459c620a3e09e0f509555def1b3b0956eccd1337405ba989ba392eb
MD5 aa6ec7854ba1524908900aa506bfb469
BLAKE2b-256 01c200514f997f4d05155fc3dc6047f494d84212231ca2f075bbed2d1540d9e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xlcalculator-0.0.4b0-py3-none-any.whl
  • Upload date:
  • Size: 85.7 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.4b0-py3-none-any.whl
Algorithm Hash digest
SHA256 862726ceb966ed92e08d7044425468e86b36d60e24ae50e22c0b88c9629d2734
MD5 c7585e2f53a3214aee6c054ae36d5d1c
BLAKE2b-256 d69357e6a0a6d21fe63944a16747cd12faee6c4f5e9645a35b510f45030b1a25

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