Skip to main content

Tools for spreadsheet analysis: formula dependency tracing, import, and cell reference conversion

Project description

spreadsheet-toolkit

Tests PyPI version Python License: MIT

Tools and utilities for spreadsheet analysis: formula dependency tracing, workbook import, and cell reference conversion.

Installation

pip install spreadsheet-toolkit

Dependencies

  • openpyxl (>=3.1.0) — for reading .xlsx files

Usage

spreadsheet_trace

Trace all the formula dependencies of a spreadsheet cell, recursively, down to the elementary value cells. The output closely mirrors the Wolfram Language Trace: each traced cell produces [cell, formula, subtraces...] and each leaf cell produces [cell, value].

from spreadsheet_toolkit import spreadsheet_trace

spreadsheet_trace("book.xlsx", "D1")
# ['D1', 'C1*2', ['C1', 'A1+B1', ['A1', 10], ['B1', 20]]]

Cell ranges are expanded, and absolute references ($B$10) are followed like plain ones:

spreadsheet_trace("sales.xlsx", "F2")
# ['F2', 'E2*$B$10', ['E2', 'C2*D2', ['C2', 25.5], ['D2', 100]], ['B10', 0.22]]

spreadsheet_trace("data.xlsx", "B1")
# ['B1', 'SUM(A1:A3)', ['A1', 15], ['A2', 22], ['A3', 8]]

Cross-sheet references and column ranges (e.g. Products!A:C, expanded across all rows of the referenced sheet) are supported:

spreadsheet_trace("report.xlsx", "Summary!B3")
# ['Summary!B3', 'Input!B14', ['Input!B14', 'SUM(B2:B13)', ['B2', 12000.0], ...]]

spreadsheet_trace("orders.xlsx", "Orders!D2")
# ['Orders!D2', 'VLOOKUP(B2,Products!A:C,3,FALSE)', ['B2', 101],
#  ['Products!A1', 'ProductID'], ['Products!A2', 101], ...]

By default duplicate dependency branches are kept, mirroring the repeated occurrences of a cell in the formulas; pass trace_duplicates=False to trace each referenced cell only once per formula:

spreadsheet_trace("data.xlsx", "C5", trace_duplicates=False)

Instead of a file path, a (sheets, data, formulas) triple as returned by import_all can be passed directly:

from spreadsheet_toolkit import import_all, spreadsheet_trace

book = import_all("book.xlsx")
spreadsheet_trace(book, "D1")

import_all

Import sheet names, cell values and formulas from a workbook, in one call. Repeated imports of the same (unmodified) file return a cached result.

from spreadsheet_toolkit import import_all

sheets, data, formulas = import_all("book.xlsx")

sheets
# ['Data', 'Summary']

data[0]       # 2D list of cell values of the first sheet ("" for empty cells)
# [[10, 20, ''], ['hello', 'world', '']]

formulas[0]   # same shape: formula strings without "=", "" for non-formula cells
# [['', '', 'A1+B1'], ['', '', '']]

index_to_position

Convert a cell reference (plain or absolute) to a (row, column) tuple with 1-based indices.

from spreadsheet_toolkit import index_to_position

index_to_position("A1")
# (1, 1)

index_to_position("C5")
# (5, 3)

index_to_position("AA1")
# (1, 27)

index_to_position("$B$10")
# (10, 2)

position_to_index

Convert a (row, column) position back to a cell reference string.

from spreadsheet_toolkit import position_to_index

position_to_index((1, 1))
# 'A1'

position_to_index((5, 3))
# 'C5'

position_to_index((1, 27))
# 'AA1'

The 0.1.x function names also remain available; see the changelog.

Performance

The Python spreadsheet_trace produces identical traces to the Wolfram Language original while running roughly 40–130× faster; see the benchmark.

See also

This Python package is a translation of the following Wolfram Language functions:

License

MIT

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

spreadsheet_toolkit-0.7.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

spreadsheet_toolkit-0.7.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file spreadsheet_toolkit-0.7.0.tar.gz.

File metadata

  • Download URL: spreadsheet_toolkit-0.7.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for spreadsheet_toolkit-0.7.0.tar.gz
Algorithm Hash digest
SHA256 3fa4b080dbaf2f33e9c81c52528cca2aa2b9c0dbfee4ddbf82c0a2e187364f07
MD5 dbbbc668037954541df0184e36636390
BLAKE2b-256 ac28c2e50a64425d6c371c0e0e66b94181e35feeafb690d5c3d4828ecb7cfe78

See more details on using hashes here.

File details

Details for the file spreadsheet_toolkit-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spreadsheet_toolkit-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c16140a21e4b108aab6596f4f45cdb4dc2f2ee5fd60d40cd2c77c03180db9a8
MD5 31bf3558864f9a10af6cd7274b5df271
BLAKE2b-256 fadd5fac76c7e368dedca8734cde048b64b6e267e58c1645c179d7aa2d556ee9

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