Skip to main content

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

Release files for formulas 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for formulas 0.1.1
File Size Uploaded
formulas-0.1.1.tar.gz 159.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for formulas 0.1.1
File Interpreter ABI Platform
formulas-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 194.8 kB

Release files / formulas-0.1.1.tar.gz

Download URL formulas-0.1.1.tar.gz
Size 159.9 kB
Tags Source
SHA-256 checksum
How to use checksums
32f99648cf6bf65cca5f29885c245418142d4d1e8d321fe551aa429578b95239
BLAKE2b-256 checksum
How to use checksums
c6ab355e5efb4776a7b28322f38e0d8feaa337e56d65b54f8ca604196358818e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / formulas-0.1.1-py3-none-any.whl

Download URL formulas-0.1.1-py3-none-any.whl
Size 34.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30ef37bb52494335ea1f529c2113d53b112881bb846796fc45f8c7152efb6d32
BLAKE2b-256 checksum
How to use checksums
8a7c6418a76537ef69af1c0736ba2424d621ea865294530d27d6090042f3f45f
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.11

2 release files

1.2.10

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

1 release file

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page