Skip to main content

Translate modern Excel formulas into OOXML syntax (e.g., _xlfn. prefixes) to work perfectly with xlsxwriter.

Project description

xlsx_formula_compiler

Translate modern Excel formulas into OOXML-compliant syntax (e.g., adding _xlfn. prefixes) for seamless use with xlsxwriter.

Installation

uv add xlsx_formula_compiler
# or
pip install xlsx_formula_compiler

Why do I need this?

Modern Excel features like Dynamic Arrays (the # operator) or functions like XLOOKUP require specific XML prefixes (_xlfn., _xlws.) to be recognized correctly by Excel when generated via libraries like xlsxwriter.

This library automates that translation so you can write formulas as they appear in the Excel formula bar.

Quick Start

from xlsx_formula_compiler import compile_formula

# Convert a dynamic array formula
raw_formula = "=A1#"
compiled = compile_formula(raw_formula)
print(compiled) 
# Output: _xlfn.ANCHORARRAY(A1)

# Use with xlsxwriter
import xlsxwriter
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_dynamic_array_formula('B1', compiled)
workbook.close()

Complex Example

The real power of xlsx_formula_compiler shows up when you combine multiple modern Excel features in a single formula. Here is a realistic report formula that uses:

  • LET — named variables compiled with the _xlpm. prefix
  • Spill references (#) — compiled to _xlfn.ANCHORARRAY
  • FILTER / SORT / UNIQUE — worksheet/future functions (compiled with _xlfn._xlws. or _xlfn.)
  • XLOOKUP — future function compiled with _xlfn.
  • Implicit intersection (@) — compiled to _xlfn.SINGLE
from xlsx_formula_compiler import compile_formula

formula = """=@LET(
    raw,        Sales[Amount],
    categories, UNIQUE(Sales[Category]),
    sorted_cat, SORT(categories),

    lookup_result, XLOOKUP(@sorted_cat,
        Products[Category],
        Products[Description],
        "N/A"
    ),

    filtered, FILTER(
        HSTACK(sorted_cat, lookup_result),
        sorted_cat <> ""
    ),

    VSTACK({"Category","Description"}, filtered)
)"""

compiled = compile_formula(formula)
print(compiled)

Output:

_xlfn.SINGLE(_xlfn.LET(
    _xlpm.raw,        Sales[Amount],
    _xlpm.categories, _xlfn.UNIQUE(Sales[Category]),
    _xlpm.sorted_cat, _xlfn._xlws.SORT(_xlpm.categories),

    _xlpm.lookup_result, _xlfn.XLOOKUP(_xlfn.SINGLE(_xlpm.sorted_cat),
        Products[Category],
        Products[Description],
        "N/A"
    ),

    _xlpm.filtered, _xlfn._xlws.FILTER(
        _xlfn.HSTACK(_xlpm.sorted_cat, _xlpm.lookup_result),
        _xlpm.sorted_cat <> ""
    ),

    _xlfn.VSTACK({"Category","Description"}, _xlpm.filtered)
))

You can then pass compiled directly to xlsxwriter:

import xlsxwriter

workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_dynamic_array_formula('A1', compiled)
workbook.close()

License

Apache License 2.0

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

xlsx_formula_compiler-0.1.4.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

xlsx_formula_compiler-0.1.4-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file xlsx_formula_compiler-0.1.4.tar.gz.

File metadata

  • Download URL: xlsx_formula_compiler-0.1.4.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xlsx_formula_compiler-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9bbfde72b30f7ff967767ba2dd53210899339f072450d5218a3bc988505a7e9b
MD5 067f101ba55bdc8cae13380d9cc5f4c8
BLAKE2b-256 bc8a4a90b62b21eb75758feac1ca62304e8514d8a960bc85f6f5b1951847a8d6

See more details on using hashes here.

File details

Details for the file xlsx_formula_compiler-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: xlsx_formula_compiler-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xlsx_formula_compiler-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8216ef7fc38f3978ef7e571a5ebb931f97557910581034ceb64ad582b550bca0
MD5 132a39095c51f52cea3eb7e724dccbf7
BLAKE2b-256 413033e6f2cdd0236cc22b9481d80931345342d9adddd544c53da1d3ab0dd0e1

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