Skip to main content

carbotek

CI PyPI Python Rust License
Ruff uv Checked with pyright Architecture: tach Typed Carbone syntax

English | Русский

Render DOCX and XLSX templates with Carbone-style markers ({d.name}, loops, conditions, formatters, images, barcodes, HTML, hyperlinks, colors) into DOCX, XLSX or PDF from Python. There is no LibreOffice: PDF is produced by a bundled copy of dxpdf with layout fixes for floating pictures (a signature over a stamp stays in place, see CARBOTEK.md).

carbotek is a Python port of the DOCX and XLSX engine of Carbone v3.2.3, the last Carbone release under the Apache License 2.0. The template syntax is compatible with Carbone 5.14.4. Behaviour was checked against the JavaScript engine with a differential corpus and formatter oracles.

carbotek is an independent project and is not affiliated with or endorsed by CarboneIO SAS. "Carbone" is used only to describe compatibility.

Licensed under the PolyForm Noncommercial 1.0.0 license. It is free for noncommercial use; commercial use requires a separate license (see LICENSE).

Installation

pip install carbotek        # or: uv add carbotek

Requires CPython 3.15+. The primary target is Linux in Docker (python:3.15-slim-trixie); CI runs the test suite against the released wheel inside that image.

Linux wheels (manylinux, x86_64 and aarch64, one abi3 wheel for CPython 3.15+) contain an optional native accelerator (carbotek._native, Rust) that renders common loop tables and money formatting faster; the output is identical to the pure-Python path. A source-distribution install builds the accelerator and needs a Rust toolchain (1.87+). Set CARBOTEK_NATIVE=0 to render without it, or CARBOTEK_NATIVE=1 to make a missing extension an import error; the variable is read once, when carbotek is imported.

A universal py3-none-any wheel is also published for environments without Rust. Pip prefers an available platform wheel; select the universal wheel explicitly to install only the Python renderer. From an unpacked source distribution, build it with uv run --no-project --with build --with setuptools --with wheel python tools/build_pure_wheel.py --out dist.

The PDF engine is part of the Linux wheels and does not depend on CARBOTEK_NATIVE. A source or universal-wheel install has no bundled engine: install carbotek[pdf-fallback] to convert with the upstream dxpdf package instead, without the layout fixes.

Library

import json
import carbotek

template = open("invoice.docx", "rb").read()
data = json.load(open("data.json", encoding="utf-8"))

docx = carbotek.render(template, data, {"lang": "en", "timezone": "Europe/London"})
pdf = carbotek.render_pdf(template, data, image_dpi=220)   # DOCX templates only

engine = carbotek.Engine(cache_size=16)   # own compiled-template cache
engine.render(template, data)
print(engine.metrics)                     # size, bytes, hits, misses, hit_rate

carbotek.render detects the template type: a DOCX template returns DOCX, an XLSX template returns XLSX. The module-level functions share one default Engine.

Options

Options are the keys of RenderOptions.create:

Key Meaning
lang Document language (default ru)
timezone IANA time zone (default Europe/Moscow)
complement {c.*} data
translations {t(...)} dictionaries
enum Enumerations for convEnum
currency {"source", "target", "rates"}
variable_str Template variables
use_high_precision_arithmetic High-precision arithmetic
export_formatted_values_as_text XLSX: keep formatted numbers as text

Russian formatters

carbotek adds formatters for Russian business documents (they are not part of Carbone):

Formatter (aliases) Example Result
digitCurrency (numCurrency, numberCurrency, digitalCurrency) {d.a:digitCurrency}, a = 1234567.5 1 234 567,50
{d.a:digitCurrency(0)} 1 234 568
formatAmount {d.a:formatAmount}, a = 1234.5 1 234,50
wordNumber (words, numberToWords) {d.a:wordNumber}, a = 1234 одна тысяча двести тридцать четыре
wordCurrency {d.a:wordCurrency}, a = 1234.56 Одна тысяча двести тридцать четыре рубля 56 копеек
moneyFull (sumFull) {d.a:moneyFull}, a = 1234.56 1 234,56 (Одна тысяча двести тридцать четыре рубля 56 копеек)
procent (percent) {d.a:procent}, a = 20 20,00 %
dashIfZero (dashZero) {d.a:dashIfZero}, a = 0 —
plural (pluralize) {d.n:plural('день','дня','дней')}, n = 5 5 дней
{d.n:plural('день','дня','дней', false)}, n = 21 день
ruDate {d.dt:ruDate}, dt = 2026-10-11 «11» октября 2026
roundHalfUp {d.a:roundHalfUp(1)}, a = 2.25 2.3
parseAmount {d.s:parseAmount}, s = 1 234,50 1234.5
parseDate {d.s:parseDate}, s = 11.10.2026 2026-10-11
capitalize (firstUpper) {d.s:capitalize}, s = иванов Иванов

Amount formatters take an optional precision (default 2) and round half up. ruDate uses the render time zone, or the one given as an argument.

Errors

All template and data errors are subclasses of carbotek.CarbotekError. Each error has a stable code:

Code Error
VALIDATION_ERROR Invalid input
TEMPLATE_ERROR Invalid template, marker or formatter
FEATURE_UNAVAILABLE Unsupported feature
CONVERTER_ERROR PDF conversion failed

Messages follow the JS engine where applicable; input and infrastructure failures have library-specific messages.

Picture geometry

Image markers use imageFit('fillWidth') by default. fillWidth and contain calculate the visible source aspect ratio after Word's a:srcRect crop, including padding from negative insets. Use {d.image:imageFit('fill')} to keep both dimensions of the template frame explicitly. An absent non-wrapping floating image is removed; inline and wrapping placeholders retain their reserved frame with a transparent PNG.

The bundled PDF engine applies picture rotation and flips about the frame's center, after cropping. It preserves per-picture transforms even when pictures share one media file. Non-wrapping pictures in table cells support page/margin references independently on each axis, including repeated table headers and mirrored logical page numbering. Paragraph and column references stay local to the cell. As in Word, a paragraph-relative position is measured from the paragraph's top edge, above its space before, and table cell margins combine per side, so a table's zero margin overrides its style's.

Images from URLs and files

By default, only data URIs are loaded. Remote and local images have to be enabled explicitly:

from carbotek.features.fetch import ImageSourceConfig

engine = carbotek.Engine(image_sources=ImageSourceConfig(
    allowlist=("cdn.example.com",),   # hosts allowed for http(s) images; private addresses are refused
    file_root="/srv/images",          # file:// images are read only under this directory
    max_bytes=10_485_760,
    fetch_timeout_ms=5000,
    max_download_concurrency=4,
    total_timeout_ms=10000,
))

Without image_sources, the same settings are read from environment variables:

  • CARBOTEK__IMAGE_URL_ALLOWLIST (comma-separated)
  • CARBOTEK__IMAGE_FILE_ROOT
  • CARBOTEK__IMAGE_MAX_BYTES
  • CARBOTEK__IMAGE_FETCH_TIMEOUT_MS
  • CARBOTEK__IMAGE_MAX_DOWNLOAD_CONCURRENCY
  • CARBOTEK__IMAGE_TOTAL_TIMEOUT_MS

Environment settings are snapshotted once at the start of each render. Changes during a render affect the next call; an explicit ImageSourceConfig takes precedence. Image limits must be positive integers; invalid environment limits raise ValidationError. Explicit configuration also validates field types and copies/normalizes the allowlist.

XLSX

XLSX templates render to XLSX the same way as in Carbone. Supported:

  • markers;
  • loops over rows and cells, including nested loops;
  • conditions and conditional blocks;
  • drop/keep (row, col, table);
  • aggregates and variables.

The workbook is prepared as in the JS engine:

  • calcChain.xml is removed;
  • shared strings become inline strings;
  • a cell whose only content is a :formatN marker becomes numeric. To keep it as text, use the export_formatted_values_as_text option or {o.exportFormattedValuesAsText=true}.

Markers work anywhere in the workbook: sheet names, headers and footers, defined names, comments.

  • Pictures: put a marker in the alt text of a sheet picture, for example {d.logo} or {d.code:barcode(ean13)}.
  • Hyperlinks: a hyperlink target https://d.site becomes {d.site:validateURL}.
  • Not recalculated: formulas, dimension and merged cells.
  • No PDF output: PDF from XLSX raises FeatureUnavailable.

CLI

carbotek render template.docx data.json -o out.docx
carbotek render template.xlsx data.json                    # -> template.out.xlsx
carbotek render template.docx data.json --pdf -o out.pdf --image-dpi 300
carbotek bench  template.docx data.json -n 50              # warm cache: cold, mean, p50, p95, min, max, per second
carbotek bench  template.docx data.json --no-cache --json   # reset module caches and registry for each render
carbotek markers template.docx                             # markers per part after validated compilation

python -m carbotek ... works too.

Common flags:

  • --lang, --timezone;
  • --options '{"complement": {"company": "X"}}';
  • - instead of the data file reads data from stdin.

Using it in a web service / task queue

The API is synchronous and CPU-bound. It takes bytes and returns bytes, and does no I/O except the optional image downloads.

  • Create one Engine per process and reuse it, so compiled templates are cached. Engine is thread-safe.
  • Do not call it on the event loop.
    • In an async framework, use await asyncio.to_thread(engine.render, template, data, options).
    • For a task queue (for example taskiq or Celery), a synchronous task with process-based workers scales best, because rendering holds the GIL.
  • Map CarbotekError to client errors (4xx) by its code. Any other exception is a server error.
  • Install the template fonts in the container (or metric-compatible fonts such as Carlito/Caladea). PDF uses system fonts. On python:3.15-slim images also install fontconfig (its configuration and libexpat are not in the image): apt-get install -y fontconfig.
import asyncio
import carbotek

engine = carbotek.Engine(cache_size=64)

async def render_document(template: bytes, data: dict, pdf: bool) -> bytes:
    if pdf:
        return await asyncio.to_thread(engine.render_pdf, template, data)
    return await asyncio.to_thread(engine.render, template, data)

Limitations

  • ECharts charts are not supported. A {d.x:chart} marker raises FeatureUnavailable. Native Word/Excel charts are updated with the data.
  • PDF has no bookmark outline and no document information (title, author).
  • Native OOXML charts are not drawn in PDF. Their data in the DOCX is still updated.
  • WMF/EMF images are drawn only partially in PDF. A warning is logged.
  • The table of contents is not recalculated. Page numbers come from the template.
  • An even/odd-page section break acts like nextPage.

Differences from Carbone

Deliberate, documented differences from the JavaScript engine:

  • A {o.*} option split across runs by Word is applied (Carbone leaves it in the text).
  • An object or array printed by a marker uses JavaScript string conversion ([object Object], 1,2).
  • A named sort combined with a repeater renders (Carbone fails to compile that template).
  • An empty native chart series is written as ptCount=0, as OOXML requires.
  • When workbook preparation removes xl/sharedStrings.xml or xl/calcChain.xml, their Override entries in [Content_Types].xml go too (Carbone keeps them, and strict readers such as openpyxl fail on the missing part).
  • Items without a named sort key keep their input order.
  • Each picture keeps its own frame size when several pictures use the same image marker (Carbone applies the size of the last of them to all).
  • HTML from :html takes its fonts and styles from the template. List bullets name no font and are drawn in the item's font (•, –, · by level; Carbone writes Symbol, Courier New and Wingdings). A missing heading style is added without a font (Carbone uses Aptos) and only for the levels the HTML uses. heading 1–heading 6, Hyperlink and Table Grid are found by name, so a template saved by a localized Word keeps using its own styles.
  • Native wheels are built only for Linux; other platforms install the pure-Python wheel, which is not tested there.

Development

uv sync                      # builds the native extension; needs the Rust toolchain from rust-toolchain.toml
                             # (without the PDF engine: PDF tests then use the dxpdf package)
uv run ruff check . && uv run ruff format --check .
uv run pyright               # strict mode, per-file headers
uv run tach check            # layer boundaries, see tach.toml
uv run pytest -n auto

Layers, from top to bottom: cli → api → pipeline, opc, pdf → features, xlsx → docx → markers, formatters → package, ooxml, options. A module may import only lower layers; tach check enforces it.

Security issues: see SECURITY.md.

License

carbotek is licensed under the PolyForm Noncommercial License 1.0.0. Copyright (c) 2026 Carbotek.

It is derived from Carbone v3.2.3, Copyright (c) CarboneIO SAS and contributors, licensed under the Apache License 2.0. The original code was translated from JavaScript to Python and Rust and substantially modified. See NOTICE and THIRD_PARTY_LICENSES.md.

Metadata

Release files for carbotek 0.2.5

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

Source distribution (sdist)

Source distribution for carbotek 0.2.5
File Size Uploaded Provenance Actions
carbotek-0.2.5.tar.gz 551.1 kB With provenance Details for carbotek-0.2.5.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for carbotek 0.2.5
File Interpreter ABI Platform Actions
carbotek-0.2.5-py3-none-any.whl Python 3 none any Details for carbotek-0.2.5-py3-none-any.whl
carbotek-0.2.5-cp315-abi3-manylinux_2_28_x86_64.whl CPython 3.15 abi3 Linux glibc 2.28+ x86-64 Details for carbotek-0.2.5-cp315-abi3-manylinux_2_28_x86_64.whl
carbotek-0.2.5-cp315-abi3-manylinux_2_28_aarch64.whl CPython 3.15 abi3 Linux glibc 2.28+ ARM64 Details for carbotek-0.2.5-cp315-abi3-manylinux_2_28_aarch64.whl

Total release size: 30.4 MB

Release files / carbotek-0.2.5.tar.gz

Download URL carbotek-0.2.5.tar.gz
Size 551.1 kB
Tags Source
SHA-256 checksum
How to use checksums
de71bb58d00bc2325a0ee9ce59343ace2f3471367c23247bc7f8e1667544faea
BLAKE2b-256 checksum
How to use checksums
3dec9bc3408f583fa6712f7c2e5ad31560211d902c8871617149d912dc48060f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / carbotek-0.2.5-py3-none-any.whl

Download URL carbotek-0.2.5-py3-none-any.whl
Size 570.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4dea3cb5c761dcfb512c96d338ef9d0a90a97321e690294f89bab0bcdc83160
BLAKE2b-256 checksum
How to use checksums
6b20effe4236d4c93f5847428b969fe4b4b7888b8ff6fb5b3b12f4149b60d29e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / carbotek-0.2.5-cp315-abi3-manylinux_2_28_x86_64.whl

Download URL carbotek-0.2.5-cp315-abi3-manylinux_2_28_x86_64.whl
Size 14.6 MB
Tags CPython 3.15 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
607aa3b79676320fb20c8ba5df555045833bbc21f01adc1861d4664271045838
BLAKE2b-256 checksum
How to use checksums
d469553cbab181c0e4b1d0e38be11b1ec579a4cdec704c32997096a7dee78aa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / carbotek-0.2.5-cp315-abi3-manylinux_2_28_aarch64.whl

Download URL carbotek-0.2.5-cp315-abi3-manylinux_2_28_aarch64.whl
Size 14.7 MB
Tags CPython 3.15 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
ce76a4ac31362977fa212d0bfb9c33d2c9a72ec99d5cae346fdd2f1fe0d89c41
BLAKE2b-256 checksum
How to use checksums
735572706ca56fe714167c35f0216d6cb7e2cf43184f1fc9d14d64e978818e1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Security for carbotek 0.2.5

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

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for carbotek-0.2.5.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 Nov 24, 2026.

Transparency log

A 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.

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