Skip to main content

Parse and compile Excel formulas and workbooks in python code.

Project description

What is formulas?

formulas implements an interpreter for Excel formulas, which parses and compile Excel formulas expressions.

Moreover, it compiles Excel workbooks to python and executes without using the Excel COM server. Hence, Excel is not needed.

Installation

To install it use (with root privileges):

$ pip install formulas

Or download the last git version and use (with root privileges):

$ python setup.py install

Install extras

Some additional functionality is enabled installing the following extras:

  • excel: enables to compile Excel workbooks to python and execute

    using: ExcelModel.

  • plot: enables to plot the formula ast and the Excel model.

To install formulas and all extras, do:

$ pip install formulas[all]

Basic Examples

The following sections will show how to:

  • parse a Excel formulas;

  • load, compile, and execute a Excel workbook;

  • extract a sub-model from a Excel workbook;

  • add a custom function.

Parsing formula

An example how to parse and execute an Excel formula is the following:

>>> import formulas
>>> func = formulas.Parser().ast('=(1 + 1) + B3 / A2')[1].compile()

To visualize formula model and get the input order you can do the following:

>>> list(func.inputs)
['A2', 'B3']
>>> func.plot(view=False)  # Set view=True to plot in the default browser.
SiteMap([(=((1 + 1) + (B3 / A2)), SiteMap())])

[graph]

Finally to execute the formula and plot the workflow:

>>> func(1, 5)
OperatorArray(7.0, dtype=object)
>>> func.plot(workflow=True, view=False)  # Set view=True to plot in the default browser.
SiteMap([(=((1 + 1) + (B3 / A2)), SiteMap())])

[graph]

Excel workbook

An example how to load, calculate, and write an Excel workbook is the following:

>>> import formulas
>>> fpath = 'file.xlsx'
>>> xl_model = formulas.ExcelModel().loads(fpath).finish()
>>> xl_model.calculate()
Solution(...)
>>> xl_model.write()
{'EXCEL.XLSX': {Book: <openpyxl.workbook.workbook.Workbook ...>}}
Tip: If you have or could have circular references, add

circular=True to finish method.

To plot the dependency graph that depict relationships between Excel cells:

>>> dsp = xl_model.dsp
>>> dsp.plot(view=False)  # Set view=True to plot in the default browser.
SiteMap([(Dispatcher ..., SiteMap())])

[graph]

To compile, execute, and plot a Excel sub-model you can do the following:

>>> inputs = ["'[EXCEL.XLSX]DATA'!A2"]  # input cells
>>> outputs = ["'[EXCEL.XLSX]DATA'!C2"]  # output cells
>>> func = xl_model.compile(inputs, outputs)
>>> func(2).value[0,0]
4.0
>>> func.plot(view=False)  # Set view=True to plot in the default browser.
SiteMap([(Dispatcher ..., SiteMap())])

[graph]

Custom functions

An example how to add a custom function to the formula parser is the following:

>>> import formulas
>>> FUNCTIONS = formulas.get_functions()
>>> FUNCTIONS['MYFUNC'] = lambda x, y: 1 + y + x
>>> func = formulas.Parser().ast('=MYFUNC(1, 2)')[1].compile()
>>> func()
4

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

formulas-0.1.3.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

formulas-0.1.3-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file formulas-0.1.3.tar.gz.

File metadata

  • Download URL: formulas-0.1.3.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.15.0 CPython/3.6.6

File hashes

Hashes for formulas-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4a4609f57e20507b03a7ef6926f95f92e46cf20a753fe70dee25deb51911d918
MD5 046e85f5fdecb037190dbb977103a61e
BLAKE2b-256 3a8a65388d9b97c57549ba9b0a01b7556cb40b24284950037e5534b3ca4dbf2f

See more details on using hashes here.

File details

Details for the file formulas-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: formulas-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 36.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.15.0 CPython/3.6.6

File hashes

Hashes for formulas-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d951cbe53d5fc3ebcd4d1e5ae0cb2425ce740c0d4c493eb51f2228b869319ffa
MD5 69b1d3b08a16cfd124a97d047a88c175
BLAKE2b-256 66101e773c986b97c2fb0f6c05af6d8bf015a07b4054453532c543eb5bf79ff7

See more details on using hashes here.

Supported by

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