Pycel

1.0b30 · active · verified Wed Apr 15

Pycel is a Python library designed to translate Excel spreadsheets into executable Python code, enabling them to run independently of Excel. It leverages a graph-based representation with caching and lazy evaluation for relatively fast execution. The library also supports visualizing the spreadsheet's dependency graph. It is currently in beta, version 1.0b30, with the last release in October 2021, and its release cadence has been irregular.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple Excel file, compile it using `pycel.ExcelCompiler`, and then evaluate cell values. It also shows how to dynamically update an input cell and observe the change in a dependent cell, illustrating `pycel`'s evaluation capabilities. The example cleans up the created Excel file afterwards.

import os
from openpyxl import Workbook
from pycel import ExcelCompiler

# Create a dummy Excel file for demonstration
workbook_name = 'example_spreadsheet.xlsx'
wb = Workbook()
ws = wb.active
ws.title = "Sheet1"
ws['A1'] = 10
ws['A2'] = 20
ws['A3'] = '=A1+A2'
ws['B1'] = 'Hello'
ws['B2'] = '=CONCATENATE(B1, " World")'
wb.save(workbook_name)

try:
    # Compile the Excel spreadsheet
    excel = ExcelCompiler(filename=workbook_name)

    # Evaluate a cell
    result_a3 = excel.evaluate('A3')
    print(f"Value of A3: {result_a3}")

    result_b2 = excel.evaluate('B2')
    print(f"Value of B2: {result_b2}")

    # Set a new value for A1 and re-evaluate A3
    excel.set_value('A1', 100)
    result_a3_updated = excel.evaluate('A3')
    print(f"Value of A3 after changing A1 to 100: {result_a3_updated}")

finally:
    # Clean up the dummy Excel file
    if os.path.exists(workbook_name):
        os.remove(workbook_name)

view raw JSON →