Skip to main content

Parse and compile excel formulas 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

Parsing

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

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

>>> import formulas
>>> fpath = 'test/test_files/excel.xlsx'
>>> xl_model = formulas.ExcelModel().loads(fpath).finish()
>>> xl_model.calculate()
Solution(...)
>>> xl_model.write()
{'EXCEL.XLSX': {Book: <openpyxl.workbook.workbook.Workbook ...>}}

To compile and execute a sub model from a workbook 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.2.tar.gz (162.2 kB view details)

Uploaded Source

Built Distribution

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

formulas-0.1.2-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: formulas-0.1.2.tar.gz
  • Upload date:
  • Size: 162.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.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for formulas-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bc5b80333694dc2183750a638b5316b3c8ae329bfa6d673b207dcb9a1b631804
MD5 746b0ae7b57fcffa0a313266d0cc1ef9
BLAKE2b-256 c8591f76710c0522db4726b1054891d005d0a00c006df59c3e6f019aa0fdd82b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: formulas-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 35.3 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.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for formulas-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2d6745766b4a3a67c66169b3857da8cd5a00a27a568f4c68cf1719bbcf27bf36
MD5 707b5ff4d38c9aa6628a15cba9291ffb
BLAKE2b-256 e82e8fbd6eb0de19d29f5acf3f080423f1e74aadd5fb5843af3c92391baec1d5

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