xpyxl — Excel in Python
Compose polished spreadsheets with pure Python—no manual coordinates. You assemble rows/columns/cells; xpyxl handles layout, rendering, and styling with utility-style classes.
Core ideas
- Positionless composition: Build sheets declaratively from
row,col,cell,table,vstack, andhstack. - Composable styling: Tailwind-inspired utilities (typography, colors, alignment, number formats) applied via
style=[...]. - Deterministic rendering: Pure-data trees compiled into
.xlsxfiles with predictable output—ideal for tests and CI diffing.
Installation
uv add xpyxl
pip install xpyxl
Getting started
import xpyxl as x
report = (
x.workbook()[
x.sheet("Summary")[
x.row(style=[x.text_2xl, x.bold, x.text_blue])["Q3 Sales Overview"],
x.row(style=[x.text_sm, x.text_gray])["Region", "Units", "Price"],
x.row(style=[x.bg_primary, x.text_white, x.bold])["EMEA", 1200, 19.0],
x.row()["APAC", 900, 21.0],
x.row()["AMER", 1500, 18.5],
]
]
)
report.save("report.xlsx")
Rendering Engines
xpyxl supports three rendering engines:
- hybrid (default): Combines xlsxwriter speed for generated sheets with openpyxl for importing existing sheets. Best balance of speed and features.
- openpyxl: Full-featured with comprehensive Excel support. Best for complex workbooks with advanced formatting.
- xlsxwriter: Fast, memory-efficient. Ideal for large datasets and performance-critical applications. Does not support
import_sheet.
Specify the engine when saving:
workbook.save("output.xlsx") # hybrid (default)
workbook.save("output.xlsx", engine="openpyxl") # full-featured
workbook.save("output.xlsx", engine="xlsxwriter") # fast, generation only
Workbook.save accepts a filesystem path, any binary buffer (like io.BytesIO()), or no target to get raw bytes:
import io
from pathlib import Path
buffer = io.BytesIO()
workbook.save(buffer, engine="xlsxwriter")
raw_bytes = workbook.save(engine="openpyxl")
Path("report.xlsx").write_bytes(raw_bytes)
Large-workbook benchmark
The process-isolated benchmarks measure wall time and operating-system peak
RSS. The first generates 2.4 million table cells across three styled sheets;
the second generates 100,000 custom report components containing 5.4 million
mixed cells across nested rows, columns, stacks, merges, formulas, and spacers.
The third exercises every OpenPyXL-backed public workflow: generated saves,
to_openpyxl(), imports, and mixed generated/imported saves through both the
OpenPyXL and hybrid engines:
uv run python scripts/benchmark_rss.py
uv run python scripts/benchmark_components_rss.py
uv run python scripts/benchmark_openpyxl.py
Use --rows-per-sheet, --sheets, --engine, and --runs to change the
table workload, or --components for the component workload; add --json for
machine-readable results. The OpenPyXL matrix accepts --rows, --import-rows,
--scenario, and --runs. For generated datasets, passing a custom re-iterable
Sequence of records to table() or components to sheet() lets xpyxl create,
render, and release content on demand. One-shot iterators cannot be streamed
because validation and rendering require repeatable passes over the data; see
GeneratedRecords and GeneratedComponents in the benchmarks for examples.
PDF and image export
Export the same report directly to PDF or PNG with a locally installed Chromium or Google Chrome browser. This print renderer is independent from the interactive HTML engine and has no required Python dependencies.
report.export("report.pdf") # Chromium is the default
report.export("summary.png", sheet="Summary")
pdf_bytes = report.export() # PDF bytes
png_bytes = report.export(format="png", sheet=0) # PNG bytes
The browser is discovered from chromium, chromium-browser,
google-chrome, or chrome. Set XPYXL_CHROMIUM_PATH or pass
chromium_path= to select another executable. PDF exports include every
sheet by default; PNG exports require sheet= when a workbook has multiple
sheets. Use scale= to produce higher-density PNG output.
For a fully in-process renderer with no browser executable, install the optional ReportLab support and select it explicitly:
pip install "xpyxl[reportlab]"
report.export("report.pdf", renderer="reportlab")
report.export("summary.png", renderer="reportlab", sheet="Summary", scale=2)
Both renderers consume the same resolved workbook layout and use the same sheet selection, canvas dimensions, cell dimensions, and value formatting. Chromium remains the default and visual reference implementation; ReportLab uses ReportLab for PDF and its Pillow dependency for PNG, entirely within the Python process.
The Chromium integration tests are opt-in because they require a locally
installed browser: XPYXL_TEST_CHROMIUM=1 uv run pytest.
Importing existing sheets
Pull in a static sheet from an existing Excel file:
report = x.workbook()[
x.import_sheet("template.xlsx", "Cover"),
x.sheet("Data", show_gridlines=False)[x.row()["Item", "Value"], x.row()["A", 1]],
]
report.save("with-template.xlsx") # uses hybrid by default (fast + imports)
Imported sheets preserve styles, merges, dimensions, freeze panes, filters, and other properties from the source file. You can override sheet gridlines with show_gridlines= on both sheet(...) and import_sheet(...).
Engine support for import_sheet:
- hybrid (default): Combines xlsxwriter speed for generated sheets with openpyxl for importing. Best balance of speed and features.
- openpyxl: Full support with native fidelity.
- xlsxwriter: Does not support
import_sheet. Usehybridoropenpyxlinstead.
Primitives
x.row(style=[x.bold, x.bg_warning])[1, 2, 3, 4, 5]
x.col(style=[x.italic])["a", "b", "c"]
x.cell(style=[x.text_green, x.number_precision])[42100]
x.cell(style=[x.bold], colspan=3)["Quarterly Summary"]
x.cell(rowspan=2)["Region"]
row[...]accepts any sequence (numbers, strings, dataclasses…)col[...]stacks values verticallycell[...]wraps a single scalarcell(...)also acceptscolspan=androwspan=for generated merged cells- All primitives accept
style=[...]
Generated merged cells work in hybrid, openpyxl, xlsxwriter, and HTML output. Merges are anchored on the cell(...) call:
hero = x.row()[
x.cell(style=[x.text_xl, x.bold, x.text_center], colspan=3)["Q3 Sales Overview"]
]
detail = x.vstack(
x.row()[x.cell(rowspan=2)["Region"], "Q1", "Q2"],
x.row()["EMEA", 1200, 1300],
)
Raw scalar values inside row()[...] remain normal 1x1 cells. Wrap a value with x.cell(...) when it should merge.
Component: table
x.table(...) renders a header + body with optional style overrides. Combine with vstack/hstack for dashboards and reports.
sales_table = x.table(
header_style=[x.text_sm, x.text_gray, x.align_middle],
style=[x.table_bordered, x.table_compact],
)[
{"Region": "EMEA", "Units": 1200, "Price": 19.0},
{"Region": "APAC", "Units": 900, "Price": 21.0},
{"Region": "AMER", "Units": 1500, "Price": 18.5},
]
layout = x.vstack(
x.row(style=[x.text_xl, x.bold])["Q3 Sales Overview"],
x.space(),
x.hstack(
sales_table,
x.cell(style=[x.text_sm, x.text_gray])["Generated with xpyxl"],
gap=2,
),
)
Tables accept polars/pandas-friendly shapes:
- records:
table()[[{"region": "EMEA", "units": 1200}, ...]]derives header from dict keys - dict of lists:
table()[{"region": ["EMEA", "APAC"], "units": [1200, 900]}]zips columns together
Utility styles
- Typography:
text_xs/_sm/_base/_lg/_xl/_2xl/_3xl,bold,italic,mono - Text colors:
text_red,text_green,text_blue,text_orange,text_purple,text_black,text_gray - Backgrounds:
bg_red,bg_primary,bg_muted,bg_success,bg_warning,bg_info - Layout & alignment:
text_left,text_center,text_right,align_top/middle/bottom,wrap,nowrap,wrap_shrink,allow_overflow,row_height(...),row_width(...) - Borders:
border_all,border_top/bottom/left/right/x/y,border_red/green/blue/...,border_thin/medium/thick,border_dashed/dotted/double/none - Tables:
table_bordered,table_banded,table_compact - Number/date formats:
number_comma,number_precision,percent,currency_usd,currency_eur,date_short,datetime_short,time_short
Layout helpers
vstack(a, b, c, gap=1, style=[x.border_all])vertically stacks components with optional blank rowshstack(a, b, gap=1, style=[x.border_all])arranges components side by side with configurable gapsspace(rows=1, height=None)inserts empty rows/columnssheet(name, background_color="#F8FAFC")sets a sheet-wide background fill
Types & ergonomics
- Modern Python with full type hints
- Pure Python stack traces; easy to debug, script, and test
- Deterministic rendering for stable diffs in CI
Cell values
Cell values render identically across all engines:
- Supported scalars —
str,int,float,bool,datetime,date,time,timedelta,Decimal, andNone. - Formula strings — any string starting with
=becomes an Excel formula (e.g."=SUM(A1:A3)"). - Anything else — other objects (e.g. dataclasses, enums) degrade to their
str()form so output never differs between engines.
Metadata
Release files for xpyxl 0.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xpyxl-0.11.0.tar.gz | 50.3 kB | With provenance | Details for xpyxl-0.11.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xpyxl-0.11.0-py3-none-any.whl | Python 3 | none | any | Details for xpyxl-0.11.0-py3-none-any.whl |
Total release size: 109.7 kB
Release files / xpyxl-0.11.0.tar.gz
| Download URL | xpyxl-0.11.0.tar.gz |
|---|---|
| Size | 50.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efe6182307fe5de3c3326bf2be1d20112812a3b27ee3938a1529dbebf2765e2d
|
|
BLAKE2b-256 checksum How to use checksums |
53b9576273a6ff75ed9de4d4747b1f690b4a1a9eef58e17f930c9248f0e787a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 13, 2026.
Transparency logRelease files / xpyxl-0.11.0-py3-none-any.whl
| Download URL | xpyxl-0.11.0-py3-none-any.whl |
|---|---|
| Size | 59.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
94fec5c563f8b79f31695b807c7ab5ef329ee84ebdb91b6d0480a21231ee1e76
|
|
BLAKE2b-256 checksum How to use checksums |
d79845c743c9ae6403b5ff73b1d41d0ee4f7a3db214ff1f8f021459cdf15ddd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 13, 2026.
Transparency logSecurity for xpyxl 0.11.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for xpyxl-0.11.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.