carbotek 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. carbotek-0.1.0/LICENSE +75 -0
  2. carbotek-0.1.0/NOTICE +13 -0
  3. carbotek-0.1.0/PKG-INFO +304 -0
  4. carbotek-0.1.0/README.md +269 -0
  5. carbotek-0.1.0/README.ru.md +235 -0
  6. carbotek-0.1.0/THIRD_PARTY_LICENSES.md +228 -0
  7. carbotek-0.1.0/licenses/Carbone-Apache-2.0.txt +201 -0
  8. carbotek-0.1.0/licenses/Unicode-3.0.txt +39 -0
  9. carbotek-0.1.0/licenses/dayjs-MIT.txt +21 -0
  10. carbotek-0.1.0/native/Cargo.lock +146 -0
  11. carbotek-0.1.0/native/Cargo.toml +21 -0
  12. carbotek-0.1.0/native/src/lib.rs +708 -0
  13. carbotek-0.1.0/pyproject.toml +70 -0
  14. carbotek-0.1.0/src/carbotek/__init__.py +32 -0
  15. carbotek-0.1.0/src/carbotek/__main__.py +6 -0
  16. carbotek-0.1.0/src/carbotek/_native.pyi +14 -0
  17. carbotek-0.1.0/src/carbotek/_native_adapter.py +100 -0
  18. carbotek-0.1.0/src/carbotek/api.py +120 -0
  19. carbotek-0.1.0/src/carbotek/cache.py +17 -0
  20. carbotek-0.1.0/src/carbotek/cli.py +195 -0
  21. carbotek-0.1.0/src/carbotek/docx/__init__.py +2 -0
  22. carbotek-0.1.0/src/carbotek/docx/blocks.py +90 -0
  23. carbotek-0.1.0/src/carbotek/docx/dialect.py +73 -0
  24. carbotek-0.1.0/src/carbotek/docx/directives.py +527 -0
  25. carbotek-0.1.0/src/carbotek/docx/loop_syntax.py +8 -0
  26. carbotek-0.1.0/src/carbotek/docx/loops.py +912 -0
  27. carbotek-0.1.0/src/carbotek/docx/normalize.py +315 -0
  28. carbotek-0.1.0/src/carbotek/docx/plan.py +102 -0
  29. carbotek-0.1.0/src/carbotek/docx/renderer.py +1897 -0
  30. carbotek-0.1.0/src/carbotek/docx/session.py +131 -0
  31. carbotek-0.1.0/src/carbotek/docx/set_markers.py +92 -0
  32. carbotek-0.1.0/src/carbotek/docx/strings.py +678 -0
  33. carbotek-0.1.0/src/carbotek/docx/text.py +62 -0
  34. carbotek-0.1.0/src/carbotek/errors.py +62 -0
  35. carbotek-0.1.0/src/carbotek/features/__init__.py +18 -0
  36. carbotek-0.1.0/src/carbotek/features/_barcode_writer.py +72 -0
  37. carbotek-0.1.0/src/carbotek/features/barcode.py +312 -0
  38. carbotek-0.1.0/src/carbotek/features/color.py +397 -0
  39. carbotek-0.1.0/src/carbotek/features/fetch.py +242 -0
  40. carbotek-0.1.0/src/carbotek/features/html.py +1467 -0
  41. carbotek-0.1.0/src/carbotek/features/hyperlinks.py +179 -0
  42. carbotek-0.1.0/src/carbotek/features/image.py +674 -0
  43. carbotek-0.1.0/src/carbotek/features/native_chart.py +844 -0
  44. carbotek-0.1.0/src/carbotek/features/template_options.py +32 -0
  45. carbotek-0.1.0/src/carbotek/formatters/__init__.py +49 -0
  46. carbotek-0.1.0/src/carbotek/formatters/aggregator.py +350 -0
  47. carbotek-0.1.0/src/carbotek/formatters/array.py +93 -0
  48. carbotek-0.1.0/src/carbotek/formatters/barcode.py +179 -0
  49. carbotek-0.1.0/src/carbotek/formatters/chart.py +45 -0
  50. carbotek-0.1.0/src/carbotek/formatters/color.py +103 -0
  51. carbotek-0.1.0/src/carbotek/formatters/condition.py +222 -0
  52. carbotek-0.1.0/src/carbotek/formatters/currencies.json +1482 -0
  53. carbotek-0.1.0/src/carbotek/formatters/date.py +507 -0
  54. carbotek-0.1.0/src/carbotek/formatters/dayjs_date_locale.json.gz +0 -0
  55. carbotek-0.1.0/src/carbotek/formatters/dayjs_relative_time.json.gz +0 -0
  56. carbotek-0.1.0/src/carbotek/formatters/docx.py +193 -0
  57. carbotek-0.1.0/src/carbotek/formatters/jsnumber.py +205 -0
  58. carbotek-0.1.0/src/carbotek/formatters/locale_currency.json +1224 -0
  59. carbotek-0.1.0/src/carbotek/formatters/locale_numbers.json +767 -0
  60. carbotek-0.1.0/src/carbotek/formatters/number.py +364 -0
  61. carbotek-0.1.0/src/carbotek/formatters/optional.py +23 -0
  62. carbotek-0.1.0/src/carbotek/formatters/region_names.json +20664 -0
  63. carbotek-0.1.0/src/carbotek/formatters/registration_order.json +1 -0
  64. carbotek-0.1.0/src/carbotek/formatters/registry.py +186 -0
  65. carbotek-0.1.0/src/carbotek/formatters/ru.py +454 -0
  66. carbotek-0.1.0/src/carbotek/formatters/set.py +130 -0
  67. carbotek-0.1.0/src/carbotek/formatters/string.py +305 -0
  68. carbotek-0.1.0/src/carbotek/formatters/timezone_names.json +1 -0
  69. carbotek-0.1.0/src/carbotek/formatters/various.py +61 -0
  70. carbotek-0.1.0/src/carbotek/markers/__init__.py +20 -0
  71. carbotek-0.1.0/src/carbotek/markers/math.py +89 -0
  72. carbotek-0.1.0/src/carbotek/markers/scan.py +37 -0
  73. carbotek-0.1.0/src/carbotek/markers/syntax.py +255 -0
  74. carbotek-0.1.0/src/carbotek/markers/variables.py +134 -0
  75. carbotek-0.1.0/src/carbotek/ooxml/__init__.py +2 -0
  76. carbotek-0.1.0/src/carbotek/ooxml/fragments.py +141 -0
  77. carbotek-0.1.0/src/carbotek/ooxml/image_types.py +39 -0
  78. carbotek-0.1.0/src/carbotek/ooxml/ns.py +28 -0
  79. carbotek-0.1.0/src/carbotek/ooxml/template_options.py +34 -0
  80. carbotek-0.1.0/src/carbotek/ooxml/xml.py +52 -0
  81. carbotek-0.1.0/src/carbotek/opc/__init__.py +7 -0
  82. carbotek-0.1.0/src/carbotek/opc/cache.py +74 -0
  83. carbotek-0.1.0/src/carbotek/opc/repository.py +117 -0
  84. carbotek-0.1.0/src/carbotek/options.py +95 -0
  85. carbotek-0.1.0/src/carbotek/package.py +122 -0
  86. carbotek-0.1.0/src/carbotek/pdf/__init__.py +6 -0
  87. carbotek-0.1.0/src/carbotek/pdf/convert.py +97 -0
  88. carbotek-0.1.0/src/carbotek/pipeline/__init__.py +2 -0
  89. carbotek-0.1.0/src/carbotek/pipeline/compile.py +256 -0
  90. carbotek-0.1.0/src/carbotek/pipeline/document.py +188 -0
  91. carbotek-0.1.0/src/carbotek/pipeline/ports.py +50 -0
  92. carbotek-0.1.0/src/carbotek/py.typed +0 -0
  93. carbotek-0.1.0/src/carbotek/render_state.py +59 -0
  94. carbotek-0.1.0/src/carbotek/typing_helpers.py +25 -0
  95. carbotek-0.1.0/src/carbotek/util.py +52 -0
  96. carbotek-0.1.0/src/carbotek/xlsx/__init__.py +6 -0
  97. carbotek-0.1.0/src/carbotek/xlsx/images.py +260 -0
  98. carbotek-0.1.0/src/carbotek/xlsx/prepare.py +313 -0
  99. carbotek-0.1.0/tools/build_pure_wheel.py +84 -0
  100. carbotek-0.1.0/tools/release_version.py +45 -0
carbotek-0.1.0/LICENSE ADDED
@@ -0,0 +1,75 @@
1
+ # PolyForm Noncommercial License 1.0.0
2
+
3
+ <https://polyformproject.org/licenses/noncommercial/1.0.0>
4
+
5
+ ## Acceptance
6
+
7
+ In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
12
+
13
+ ## Distribution License
14
+
15
+ The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
16
+
17
+ ## Notices
18
+
19
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
20
+
21
+ > Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
22
+
23
+ ## Changes and New Works License
24
+
25
+ The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
26
+
27
+ ## Patent License
28
+
29
+ The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
30
+
31
+ ## Noncommercial Purposes
32
+
33
+ Any noncommercial purpose is a permitted purpose.
34
+
35
+ ## Personal Uses
36
+
37
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
38
+
39
+ ## Noncommercial Organizations
40
+
41
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
42
+
43
+ ## Fair Use
44
+
45
+ You may have "fair use" rights for the software under the law. These terms do not limit them.
46
+
47
+ ## No Other Rights
48
+
49
+ These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
50
+
51
+ ## Patent Defense
52
+
53
+ If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
54
+
55
+ ## Violations
56
+
57
+ The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
58
+
59
+ ## No Liability
60
+
61
+ ***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
62
+
63
+ ## Definitions
64
+
65
+ The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
66
+
67
+ **You** refers to the individual or entity agreeing to these terms.
68
+
69
+ **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
70
+
71
+ **Your licenses** are all the licenses granted to you for the software under these terms.
72
+
73
+ **Use** means anything you do with the software requiring one of your licenses.
74
+
75
+ Required Notice: Copyright (c) 2026 Carbotek
carbotek-0.1.0/NOTICE ADDED
@@ -0,0 +1,13 @@
1
+ carbotek
2
+ Copyright (c) 2026 Carbotek
3
+
4
+ Licensed under the PolyForm Noncommercial License 1.0.0 (see LICENSE).
5
+ Commercial use requires a separate license from the copyright holder.
6
+
7
+ This product is derived from Carbone v3.2.3
8
+ (https://github.com/carboneio/carbone),
9
+ Copyright (c) CarboneIO SAS and contributors,
10
+ licensed under the Apache License, Version 2.0 (see licenses/Carbone-Apache-2.0.txt).
11
+ The original JavaScript code was translated to Python and Rust and substantially modified.
12
+
13
+ carbotek is not affiliated with or endorsed by CarboneIO SAS.
@@ -0,0 +1,304 @@
1
+ Metadata-Version: 2.4
2
+ Name: carbotek
3
+ Version: 0.1.0
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: Operating System :: POSIX :: Linux
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.15
9
+ Classifier: Programming Language :: Rust
10
+ Classifier: Topic :: Office/Business :: Office Suites
11
+ Classifier: Topic :: Text Processing :: Markup :: XML
12
+ Classifier: Typing :: Typed
13
+ Requires-Dist: dxpdf>=0.8,<1
14
+ Requires-Dist: lxml>=5.3,<7
15
+ Requires-Dist: pillow>=11,<13
16
+ Requires-Dist: python-barcode>=0.16,<1
17
+ Requires-Dist: resvg-py>=0.5,<0.6
18
+ Requires-Dist: segno>=1.6,<2
19
+ Requires-Dist: tzdata>=2025.2
20
+ License-File: LICENSE
21
+ License-File: NOTICE
22
+ License-File: THIRD_PARTY_LICENSES.md
23
+ License-File: licenses/Carbone-Apache-2.0.txt
24
+ License-File: licenses/Unicode-3.0.txt
25
+ License-File: licenses/dayjs-MIT.txt
26
+ Summary: Render DOCX/XLSX templates with Carbone-style markers to DOCX, XLSX or PDF, without LibreOffice
27
+ Keywords: docx,xlsx,pdf,template,report,carbone,document-generation
28
+ Author-email: Carbotek <337455888+carbotek@users.noreply.github.com>
29
+ License-Expression: PolyForm-Noncommercial-1.0.0
30
+ Requires-Python: >=3.15
31
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
32
+ Project-URL: Homepage, https://github.com/carbotek/carbotek
33
+ Project-URL: Issues, https://github.com/carbotek/carbotek/issues
34
+
35
+ # carbotek
36
+
37
+ <p align="center">
38
+ <a href="https://github.com/carbotek/carbotek/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/carbotek/carbotek/actions/workflows/ci.yml/badge.svg"></a>
39
+ <a href="https://pypi.org/project/carbotek/"><img alt="PyPI" src="https://img.shields.io/pypi/v/carbotek?color=blue"></a>
40
+ <img alt="Python" src="https://img.shields.io/badge/python-3.15%2B-3776AB?logo=python&logoColor=white">
41
+ <img alt="Rust" src="https://img.shields.io/badge/native-Rust%201.99-CE422B?logo=rust&logoColor=white">
42
+ <a href="https://github.com/carbotek/carbotek/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/license-PolyForm%20Noncommercial%201.0.0-orange"></a>
43
+ <br>
44
+ <a href="https://github.com/astral-sh/ruff"><img alt="Ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json"></a>
45
+ <a href="https://github.com/astral-sh/uv"><img alt="uv" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json"></a>
46
+ <a href="https://microsoft.github.io/pyright/"><img alt="Checked with pyright" src="https://microsoft.github.io/pyright/img/pyright_badge.svg"></a>
47
+ <a href="https://docs.gauge.sh"><img alt="Architecture: tach" src="https://img.shields.io/badge/architecture-tach-6C47FF"></a>
48
+ <img alt="Typed" src="https://img.shields.io/badge/typing-typed-brightgreen">
49
+ <img alt="Carbone syntax" src="https://img.shields.io/badge/Carbone%20syntax-5.14.4-2E7D32">
50
+ </p>
51
+
52
+ **English** | [Русский](https://github.com/carbotek/carbotek/blob/main/README.ru.md)
53
+
54
+ Render DOCX and XLSX templates with Carbone-style markers (`{d.name}`, loops, conditions,
55
+ formatters, images, barcodes, HTML, hyperlinks, colors) into DOCX, XLSX or PDF from Python.
56
+ There is no LibreOffice: PDF is produced by [`dxpdf`](https://pypi.org/project/dxpdf/).
57
+
58
+ carbotek is a Python port of the DOCX and XLSX engine of [Carbone](https://github.com/carboneio/carbone)
59
+ **v3.2.3**, the last Carbone release under the Apache License 2.0. The template syntax is compatible
60
+ with Carbone **5.14.4**. Behaviour was checked against the JavaScript engine with a differential
61
+ corpus and formatter oracles.
62
+
63
+ carbotek is an independent project and is not affiliated with or endorsed by CarboneIO SAS.
64
+ "Carbone" is used only to describe compatibility.
65
+
66
+ > Licensed under the **PolyForm Noncommercial 1.0.0** license. It is free for noncommercial
67
+ > use; commercial use requires a separate license (see [LICENSE](https://github.com/carbotek/carbotek/blob/main/LICENSE)).
68
+
69
+ ## Installation
70
+
71
+ ```bash
72
+ pip install carbotek # or: uv add carbotek
73
+ ```
74
+
75
+ Requires CPython 3.15+. The primary target is Linux in Docker (`python:3.15-slim-trixie`); CI runs
76
+ the test suite against the released wheel inside that image.
77
+
78
+ 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
79
+ loop tables and money formatting faster; the output is identical to the pure-Python path. A
80
+ 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
81
+ missing extension an import error; the variable is read once, when `carbotek` is imported.
82
+
83
+ A universal `py3-none-any` wheel is also published for environments without Rust. Pip prefers
84
+ an available platform wheel; select the universal wheel explicitly to install only the Python
85
+ renderer. From an unpacked source distribution, build it with
86
+ `uv run --no-project --with build --with setuptools --with wheel python tools/build_pure_wheel.py --out dist`.
87
+
88
+ ## Library
89
+
90
+ ```python
91
+ import json
92
+ import carbotek
93
+
94
+ template = open("invoice.docx", "rb").read()
95
+ data = json.load(open("data.json", encoding="utf-8"))
96
+
97
+ docx = carbotek.render(template, data, {"lang": "en", "timezone": "Europe/London"})
98
+ pdf = carbotek.render_pdf(template, data, image_dpi=220) # DOCX templates only
99
+
100
+ engine = carbotek.Engine(cache_size=16) # own compiled-template cache
101
+ engine.render(template, data)
102
+ print(engine.metrics) # size, bytes, hits, misses, hit_rate
103
+ ```
104
+
105
+ `carbotek.render` detects the template type: a DOCX template returns DOCX, an XLSX template
106
+ returns XLSX. The module-level functions share one default `Engine`.
107
+
108
+ ### Options
109
+
110
+ Options are the keys of `RenderOptions.create`:
111
+
112
+ | Key | Meaning |
113
+ |---|---|
114
+ | `lang` | Document language (default `ru`) |
115
+ | `timezone` | IANA time zone (default `Europe/Moscow`) |
116
+ | `complement` | `{c.*}` data |
117
+ | `translations` | `{t(...)}` dictionaries |
118
+ | `enum` | Enumerations for `convEnum` |
119
+ | `currency` | `{"source", "target", "rates"}` |
120
+ | `variable_str` | Template variables |
121
+ | `use_high_precision_arithmetic` | High-precision arithmetic |
122
+ | `export_formatted_values_as_text` | XLSX: keep formatted numbers as text |
123
+
124
+ ### Russian formatters
125
+
126
+ carbotek adds formatters for Russian business documents (they are not part of Carbone):
127
+
128
+ | Formatter (aliases) | Example | Result |
129
+ |---|---|---|
130
+ | `digitCurrency` (`numCurrency`, `numberCurrency`, `digitalCurrency`) | `{d.a:digitCurrency}`, a = 1234567.5 | `1 234 567,50` |
131
+ | | `{d.a:digitCurrency(0)}` | `1 234 568` |
132
+ | `formatAmount` | `{d.a:formatAmount}`, a = 1234.5 | `1 234,50` |
133
+ | `wordNumber` (`words`, `numberToWords`) | `{d.a:wordNumber}`, a = 1234 | `одна тысяча двести тридцать четыре` |
134
+ | `wordCurrency` | `{d.a:wordCurrency}`, a = 1234.56 | `Одна тысяча двести тридцать четыре рубля 56 копеек` |
135
+ | `moneyFull` (`sumFull`) | `{d.a:moneyFull}`, a = 1234.56 | `1 234,56 (Одна тысяча двести тридцать четыре рубля 56 копеек)` |
136
+ | `procent` (`percent`) | `{d.a:procent}`, a = 20 | `20,00 %` |
137
+ | `dashIfZero` (`dashZero`) | `{d.a:dashIfZero}`, a = 0 | `—` |
138
+ | `plural` (`pluralize`) | `{d.n:plural('день','дня','дней')}`, n = 5 | `5 дней` |
139
+ | | `{d.n:plural('день','дня','дней', false)}`, n = 21 | `день` |
140
+ | `ruDate` | `{d.dt:ruDate}`, dt = `2026-10-11` | `«11» октября 2026` |
141
+ | `roundHalfUp` | `{d.a:roundHalfUp(1)}`, a = 2.25 | `2.3` |
142
+ | `parseAmount` | `{d.s:parseAmount}`, s = `1 234,50` | `1234.5` |
143
+ | `parseDate` | `{d.s:parseDate}`, s = `11.10.2026` | `2026-10-11` |
144
+ | `capitalize` (`firstUpper`) | `{d.s:capitalize}`, s = `иванов` | `Иванов` |
145
+
146
+ Amount formatters take an optional precision (default 2) and round half up. `ruDate` uses the
147
+ render time zone, or the one given as an argument.
148
+
149
+ ### Errors
150
+
151
+ All template and data errors are subclasses of `carbotek.CarbotekError`. Each error has a stable
152
+ `code`:
153
+
154
+ | Code | Error |
155
+ |---|---|
156
+ | `VALIDATION_ERROR` | Invalid input |
157
+ | `TEMPLATE_ERROR` | Invalid template, marker or formatter |
158
+ | `FEATURE_UNAVAILABLE` | Unsupported feature |
159
+ | `CONVERTER_ERROR` | PDF conversion failed |
160
+
161
+ Messages follow the JS engine where applicable; input and infrastructure failures have library-specific messages.
162
+
163
+ ### Images from URLs and files
164
+
165
+ By default, only data URIs are loaded. Remote and local images have to be enabled explicitly:
166
+
167
+ ```python
168
+ from carbotek.features.fetch import ImageSourceConfig
169
+
170
+ engine = carbotek.Engine(image_sources=ImageSourceConfig(
171
+ allowlist=("cdn.example.com",), # hosts allowed for http(s) images; private addresses are refused
172
+ file_root="/srv/images", # file:// images are read only under this directory
173
+ max_bytes=10_485_760,
174
+ fetch_timeout_ms=5000,
175
+ max_download_concurrency=4,
176
+ total_timeout_ms=10000,
177
+ ))
178
+ ```
179
+
180
+ Without `image_sources`, the same settings are read from environment variables:
181
+
182
+ - `CARBOTEK__IMAGE_URL_ALLOWLIST` (comma-separated)
183
+ - `CARBOTEK__IMAGE_FILE_ROOT`
184
+ - `CARBOTEK__IMAGE_MAX_BYTES`
185
+ - `CARBOTEK__IMAGE_FETCH_TIMEOUT_MS`
186
+ - `CARBOTEK__IMAGE_MAX_DOWNLOAD_CONCURRENCY`
187
+ - `CARBOTEK__IMAGE_TOTAL_TIMEOUT_MS`
188
+
189
+ 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.
190
+
191
+ ## XLSX
192
+
193
+ XLSX templates render to XLSX the same way as in Carbone. Supported:
194
+
195
+ - markers;
196
+ - loops over rows and cells, including nested loops;
197
+ - conditions and conditional blocks;
198
+ - `drop`/`keep` (`row`, `col`, `table`);
199
+ - aggregates and variables.
200
+
201
+ The workbook is prepared as in the JS engine:
202
+
203
+ - `calcChain.xml` is removed;
204
+ - shared strings become inline strings;
205
+ - 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}`.
206
+
207
+ Markers work anywhere in the workbook: sheet names, headers and footers, defined names, comments.
208
+
209
+ - **Pictures:** put a marker in the alt text of a sheet picture, for example `{d.logo}` or `{d.code:barcode(ean13)}`.
210
+ - **Hyperlinks:** a hyperlink target `https://d.site` becomes `{d.site:validateURL}`.
211
+ - **Not recalculated:** formulas, `dimension` and merged cells.
212
+ - **No PDF output:** PDF from XLSX raises `FeatureUnavailable`.
213
+
214
+ ## CLI
215
+
216
+ ```bash
217
+ carbotek render template.docx data.json -o out.docx
218
+ carbotek render template.xlsx data.json # -> template.out.xlsx
219
+ carbotek render template.docx data.json --pdf -o out.pdf --image-dpi 300
220
+ carbotek bench template.docx data.json -n 50 # warm cache: cold, mean, p50, p95, min, max, per second
221
+ carbotek bench template.docx data.json --no-cache --json # reset module caches and registry for each render
222
+ carbotek markers template.docx # markers per part after validated compilation
223
+ ```
224
+
225
+ `python -m carbotek ...` works too.
226
+
227
+ Common flags:
228
+
229
+ - `--lang`, `--timezone`;
230
+ - `--options '{"complement": {"company": "X"}}'`;
231
+ - `-` instead of the data file reads data from stdin.
232
+
233
+ ## Using it in a web service / task queue
234
+
235
+ The API is synchronous and CPU-bound. It takes bytes and returns bytes, and does no I/O except
236
+ the optional image downloads.
237
+
238
+ - **Create one `Engine` per process** and reuse it, so compiled templates are cached. `Engine` is thread-safe.
239
+ - **Do not call it on the event loop.**
240
+ - In an async framework, use `await asyncio.to_thread(engine.render, template, data, options)`.
241
+ - For a task queue (for example taskiq or Celery), a synchronous task with process-based workers scales best, because rendering holds the GIL.
242
+ - **Map `CarbotekError` to client errors (4xx)** by its `code`. Any other exception is a server error.
243
+ - **Install the template fonts in the container** (or metric-compatible fonts such as Carlito/Caladea). PDF uses system fonts.
244
+
245
+ ```python
246
+ import asyncio
247
+ import carbotek
248
+
249
+ engine = carbotek.Engine(cache_size=64)
250
+
251
+ async def render_document(template: bytes, data: dict, pdf: bool) -> bytes:
252
+ if pdf:
253
+ return await asyncio.to_thread(engine.render_pdf, template, data)
254
+ return await asyncio.to_thread(engine.render, template, data)
255
+ ```
256
+
257
+ ## Limitations
258
+
259
+ - **ECharts charts are not supported.** A `{d.x:chart}` marker raises `FeatureUnavailable`. Native Word/Excel charts are updated with the data.
260
+ - **PDF has no bookmark outline and no document information** (title, author).
261
+ - **Native OOXML charts are not drawn in PDF.** Their data in the DOCX is still updated.
262
+ - **WMF/EMF images are drawn only partially in PDF.** A warning is logged.
263
+ - **The table of contents is not recalculated.** Page numbers come from the template.
264
+ - **An even/odd-page section break acts like `nextPage`.**
265
+
266
+ ## Differences from Carbone
267
+
268
+ Deliberate, documented differences from the JavaScript engine:
269
+
270
+ - A `{o.*}` option split across runs by Word is applied (Carbone leaves it in the text).
271
+ - An object or array printed by a marker uses JavaScript string conversion (`[object Object]`, `1,2`).
272
+ - A named sort combined with a repeater renders (Carbone fails to compile that template).
273
+ - An empty native chart series is written as `ptCount=0`, as OOXML requires.
274
+ - Items without a named sort key keep their input order.
275
+ - Each picture keeps its own frame size when several pictures use the same image marker (Carbone
276
+ applies the size of the last of them to all).
277
+ - Native wheels are built only for Linux; other platforms install the pure-Python wheel, which is not tested there.
278
+
279
+ ## Development
280
+
281
+ ```bash
282
+ uv sync # builds the native extension; needs the Rust toolchain from rust-toolchain.toml
283
+ uv run ruff check . && uv run ruff format --check .
284
+ uv run pyright # strict mode, per-file headers
285
+ uv run tach check # layer boundaries, see tach.toml
286
+ uv run pytest -n auto
287
+ ```
288
+
289
+ Layers, from top to bottom: `cli` → `api` → `pipeline`, `opc`, `pdf` → `features`, `xlsx` → `docx`
290
+ → `markers`, `formatters` → `package`, `ooxml`, `options`. A module may import only lower layers;
291
+ `tach check` enforces it.
292
+
293
+ Security issues: see [SECURITY.md](https://github.com/carbotek/carbotek/blob/main/SECURITY.md).
294
+
295
+ ## License
296
+
297
+ carbotek is licensed under the [PolyForm Noncommercial License 1.0.0](https://github.com/carbotek/carbotek/blob/main/LICENSE).
298
+ Copyright (c) 2026 Carbotek.
299
+
300
+ It is derived from Carbone v3.2.3, Copyright (c) CarboneIO SAS and contributors, licensed under
301
+ the [Apache License 2.0](https://github.com/carbotek/carbotek/blob/main/licenses/Carbone-Apache-2.0.txt). The original code was translated from
302
+ JavaScript to Python and Rust and substantially modified. See [NOTICE](https://github.com/carbotek/carbotek/blob/main/NOTICE) and
303
+ [THIRD_PARTY_LICENSES.md](https://github.com/carbotek/carbotek/blob/main/THIRD_PARTY_LICENSES.md).
304
+
@@ -0,0 +1,269 @@
1
+ # carbotek
2
+
3
+ <p align="center">
4
+ <a href="https://github.com/carbotek/carbotek/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/carbotek/carbotek/actions/workflows/ci.yml/badge.svg"></a>
5
+ <a href="https://pypi.org/project/carbotek/"><img alt="PyPI" src="https://img.shields.io/pypi/v/carbotek?color=blue"></a>
6
+ <img alt="Python" src="https://img.shields.io/badge/python-3.15%2B-3776AB?logo=python&logoColor=white">
7
+ <img alt="Rust" src="https://img.shields.io/badge/native-Rust%201.99-CE422B?logo=rust&logoColor=white">
8
+ <a href="https://github.com/carbotek/carbotek/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/license-PolyForm%20Noncommercial%201.0.0-orange"></a>
9
+ <br>
10
+ <a href="https://github.com/astral-sh/ruff"><img alt="Ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json"></a>
11
+ <a href="https://github.com/astral-sh/uv"><img alt="uv" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json"></a>
12
+ <a href="https://microsoft.github.io/pyright/"><img alt="Checked with pyright" src="https://microsoft.github.io/pyright/img/pyright_badge.svg"></a>
13
+ <a href="https://docs.gauge.sh"><img alt="Architecture: tach" src="https://img.shields.io/badge/architecture-tach-6C47FF"></a>
14
+ <img alt="Typed" src="https://img.shields.io/badge/typing-typed-brightgreen">
15
+ <img alt="Carbone syntax" src="https://img.shields.io/badge/Carbone%20syntax-5.14.4-2E7D32">
16
+ </p>
17
+
18
+ **English** | [Русский](https://github.com/carbotek/carbotek/blob/main/README.ru.md)
19
+
20
+ Render DOCX and XLSX templates with Carbone-style markers (`{d.name}`, loops, conditions,
21
+ formatters, images, barcodes, HTML, hyperlinks, colors) into DOCX, XLSX or PDF from Python.
22
+ There is no LibreOffice: PDF is produced by [`dxpdf`](https://pypi.org/project/dxpdf/).
23
+
24
+ carbotek is a Python port of the DOCX and XLSX engine of [Carbone](https://github.com/carboneio/carbone)
25
+ **v3.2.3**, the last Carbone release under the Apache License 2.0. The template syntax is compatible
26
+ with Carbone **5.14.4**. Behaviour was checked against the JavaScript engine with a differential
27
+ corpus and formatter oracles.
28
+
29
+ carbotek is an independent project and is not affiliated with or endorsed by CarboneIO SAS.
30
+ "Carbone" is used only to describe compatibility.
31
+
32
+ > Licensed under the **PolyForm Noncommercial 1.0.0** license. It is free for noncommercial
33
+ > use; commercial use requires a separate license (see [LICENSE](https://github.com/carbotek/carbotek/blob/main/LICENSE)).
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install carbotek # or: uv add carbotek
39
+ ```
40
+
41
+ Requires CPython 3.15+. The primary target is Linux in Docker (`python:3.15-slim-trixie`); CI runs
42
+ the test suite against the released wheel inside that image.
43
+
44
+ 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
45
+ loop tables and money formatting faster; the output is identical to the pure-Python path. A
46
+ 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
47
+ missing extension an import error; the variable is read once, when `carbotek` is imported.
48
+
49
+ A universal `py3-none-any` wheel is also published for environments without Rust. Pip prefers
50
+ an available platform wheel; select the universal wheel explicitly to install only the Python
51
+ renderer. From an unpacked source distribution, build it with
52
+ `uv run --no-project --with build --with setuptools --with wheel python tools/build_pure_wheel.py --out dist`.
53
+
54
+ ## Library
55
+
56
+ ```python
57
+ import json
58
+ import carbotek
59
+
60
+ template = open("invoice.docx", "rb").read()
61
+ data = json.load(open("data.json", encoding="utf-8"))
62
+
63
+ docx = carbotek.render(template, data, {"lang": "en", "timezone": "Europe/London"})
64
+ pdf = carbotek.render_pdf(template, data, image_dpi=220) # DOCX templates only
65
+
66
+ engine = carbotek.Engine(cache_size=16) # own compiled-template cache
67
+ engine.render(template, data)
68
+ print(engine.metrics) # size, bytes, hits, misses, hit_rate
69
+ ```
70
+
71
+ `carbotek.render` detects the template type: a DOCX template returns DOCX, an XLSX template
72
+ returns XLSX. The module-level functions share one default `Engine`.
73
+
74
+ ### Options
75
+
76
+ Options are the keys of `RenderOptions.create`:
77
+
78
+ | Key | Meaning |
79
+ |---|---|
80
+ | `lang` | Document language (default `ru`) |
81
+ | `timezone` | IANA time zone (default `Europe/Moscow`) |
82
+ | `complement` | `{c.*}` data |
83
+ | `translations` | `{t(...)}` dictionaries |
84
+ | `enum` | Enumerations for `convEnum` |
85
+ | `currency` | `{"source", "target", "rates"}` |
86
+ | `variable_str` | Template variables |
87
+ | `use_high_precision_arithmetic` | High-precision arithmetic |
88
+ | `export_formatted_values_as_text` | XLSX: keep formatted numbers as text |
89
+
90
+ ### Russian formatters
91
+
92
+ carbotek adds formatters for Russian business documents (they are not part of Carbone):
93
+
94
+ | Formatter (aliases) | Example | Result |
95
+ |---|---|---|
96
+ | `digitCurrency` (`numCurrency`, `numberCurrency`, `digitalCurrency`) | `{d.a:digitCurrency}`, a = 1234567.5 | `1 234 567,50` |
97
+ | | `{d.a:digitCurrency(0)}` | `1 234 568` |
98
+ | `formatAmount` | `{d.a:formatAmount}`, a = 1234.5 | `1 234,50` |
99
+ | `wordNumber` (`words`, `numberToWords`) | `{d.a:wordNumber}`, a = 1234 | `одна тысяча двести тридцать четыре` |
100
+ | `wordCurrency` | `{d.a:wordCurrency}`, a = 1234.56 | `Одна тысяча двести тридцать четыре рубля 56 копеек` |
101
+ | `moneyFull` (`sumFull`) | `{d.a:moneyFull}`, a = 1234.56 | `1 234,56 (Одна тысяча двести тридцать четыре рубля 56 копеек)` |
102
+ | `procent` (`percent`) | `{d.a:procent}`, a = 20 | `20,00 %` |
103
+ | `dashIfZero` (`dashZero`) | `{d.a:dashIfZero}`, a = 0 | `—` |
104
+ | `plural` (`pluralize`) | `{d.n:plural('день','дня','дней')}`, n = 5 | `5 дней` |
105
+ | | `{d.n:plural('день','дня','дней', false)}`, n = 21 | `день` |
106
+ | `ruDate` | `{d.dt:ruDate}`, dt = `2026-10-11` | `«11» октября 2026` |
107
+ | `roundHalfUp` | `{d.a:roundHalfUp(1)}`, a = 2.25 | `2.3` |
108
+ | `parseAmount` | `{d.s:parseAmount}`, s = `1 234,50` | `1234.5` |
109
+ | `parseDate` | `{d.s:parseDate}`, s = `11.10.2026` | `2026-10-11` |
110
+ | `capitalize` (`firstUpper`) | `{d.s:capitalize}`, s = `иванов` | `Иванов` |
111
+
112
+ Amount formatters take an optional precision (default 2) and round half up. `ruDate` uses the
113
+ render time zone, or the one given as an argument.
114
+
115
+ ### Errors
116
+
117
+ All template and data errors are subclasses of `carbotek.CarbotekError`. Each error has a stable
118
+ `code`:
119
+
120
+ | Code | Error |
121
+ |---|---|
122
+ | `VALIDATION_ERROR` | Invalid input |
123
+ | `TEMPLATE_ERROR` | Invalid template, marker or formatter |
124
+ | `FEATURE_UNAVAILABLE` | Unsupported feature |
125
+ | `CONVERTER_ERROR` | PDF conversion failed |
126
+
127
+ Messages follow the JS engine where applicable; input and infrastructure failures have library-specific messages.
128
+
129
+ ### Images from URLs and files
130
+
131
+ By default, only data URIs are loaded. Remote and local images have to be enabled explicitly:
132
+
133
+ ```python
134
+ from carbotek.features.fetch import ImageSourceConfig
135
+
136
+ engine = carbotek.Engine(image_sources=ImageSourceConfig(
137
+ allowlist=("cdn.example.com",), # hosts allowed for http(s) images; private addresses are refused
138
+ file_root="/srv/images", # file:// images are read only under this directory
139
+ max_bytes=10_485_760,
140
+ fetch_timeout_ms=5000,
141
+ max_download_concurrency=4,
142
+ total_timeout_ms=10000,
143
+ ))
144
+ ```
145
+
146
+ Without `image_sources`, the same settings are read from environment variables:
147
+
148
+ - `CARBOTEK__IMAGE_URL_ALLOWLIST` (comma-separated)
149
+ - `CARBOTEK__IMAGE_FILE_ROOT`
150
+ - `CARBOTEK__IMAGE_MAX_BYTES`
151
+ - `CARBOTEK__IMAGE_FETCH_TIMEOUT_MS`
152
+ - `CARBOTEK__IMAGE_MAX_DOWNLOAD_CONCURRENCY`
153
+ - `CARBOTEK__IMAGE_TOTAL_TIMEOUT_MS`
154
+
155
+ 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.
156
+
157
+ ## XLSX
158
+
159
+ XLSX templates render to XLSX the same way as in Carbone. Supported:
160
+
161
+ - markers;
162
+ - loops over rows and cells, including nested loops;
163
+ - conditions and conditional blocks;
164
+ - `drop`/`keep` (`row`, `col`, `table`);
165
+ - aggregates and variables.
166
+
167
+ The workbook is prepared as in the JS engine:
168
+
169
+ - `calcChain.xml` is removed;
170
+ - shared strings become inline strings;
171
+ - 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}`.
172
+
173
+ Markers work anywhere in the workbook: sheet names, headers and footers, defined names, comments.
174
+
175
+ - **Pictures:** put a marker in the alt text of a sheet picture, for example `{d.logo}` or `{d.code:barcode(ean13)}`.
176
+ - **Hyperlinks:** a hyperlink target `https://d.site` becomes `{d.site:validateURL}`.
177
+ - **Not recalculated:** formulas, `dimension` and merged cells.
178
+ - **No PDF output:** PDF from XLSX raises `FeatureUnavailable`.
179
+
180
+ ## CLI
181
+
182
+ ```bash
183
+ carbotek render template.docx data.json -o out.docx
184
+ carbotek render template.xlsx data.json # -> template.out.xlsx
185
+ carbotek render template.docx data.json --pdf -o out.pdf --image-dpi 300
186
+ carbotek bench template.docx data.json -n 50 # warm cache: cold, mean, p50, p95, min, max, per second
187
+ carbotek bench template.docx data.json --no-cache --json # reset module caches and registry for each render
188
+ carbotek markers template.docx # markers per part after validated compilation
189
+ ```
190
+
191
+ `python -m carbotek ...` works too.
192
+
193
+ Common flags:
194
+
195
+ - `--lang`, `--timezone`;
196
+ - `--options '{"complement": {"company": "X"}}'`;
197
+ - `-` instead of the data file reads data from stdin.
198
+
199
+ ## Using it in a web service / task queue
200
+
201
+ The API is synchronous and CPU-bound. It takes bytes and returns bytes, and does no I/O except
202
+ the optional image downloads.
203
+
204
+ - **Create one `Engine` per process** and reuse it, so compiled templates are cached. `Engine` is thread-safe.
205
+ - **Do not call it on the event loop.**
206
+ - In an async framework, use `await asyncio.to_thread(engine.render, template, data, options)`.
207
+ - For a task queue (for example taskiq or Celery), a synchronous task with process-based workers scales best, because rendering holds the GIL.
208
+ - **Map `CarbotekError` to client errors (4xx)** by its `code`. Any other exception is a server error.
209
+ - **Install the template fonts in the container** (or metric-compatible fonts such as Carlito/Caladea). PDF uses system fonts.
210
+
211
+ ```python
212
+ import asyncio
213
+ import carbotek
214
+
215
+ engine = carbotek.Engine(cache_size=64)
216
+
217
+ async def render_document(template: bytes, data: dict, pdf: bool) -> bytes:
218
+ if pdf:
219
+ return await asyncio.to_thread(engine.render_pdf, template, data)
220
+ return await asyncio.to_thread(engine.render, template, data)
221
+ ```
222
+
223
+ ## Limitations
224
+
225
+ - **ECharts charts are not supported.** A `{d.x:chart}` marker raises `FeatureUnavailable`. Native Word/Excel charts are updated with the data.
226
+ - **PDF has no bookmark outline and no document information** (title, author).
227
+ - **Native OOXML charts are not drawn in PDF.** Their data in the DOCX is still updated.
228
+ - **WMF/EMF images are drawn only partially in PDF.** A warning is logged.
229
+ - **The table of contents is not recalculated.** Page numbers come from the template.
230
+ - **An even/odd-page section break acts like `nextPage`.**
231
+
232
+ ## Differences from Carbone
233
+
234
+ Deliberate, documented differences from the JavaScript engine:
235
+
236
+ - A `{o.*}` option split across runs by Word is applied (Carbone leaves it in the text).
237
+ - An object or array printed by a marker uses JavaScript string conversion (`[object Object]`, `1,2`).
238
+ - A named sort combined with a repeater renders (Carbone fails to compile that template).
239
+ - An empty native chart series is written as `ptCount=0`, as OOXML requires.
240
+ - Items without a named sort key keep their input order.
241
+ - Each picture keeps its own frame size when several pictures use the same image marker (Carbone
242
+ applies the size of the last of them to all).
243
+ - Native wheels are built only for Linux; other platforms install the pure-Python wheel, which is not tested there.
244
+
245
+ ## Development
246
+
247
+ ```bash
248
+ uv sync # builds the native extension; needs the Rust toolchain from rust-toolchain.toml
249
+ uv run ruff check . && uv run ruff format --check .
250
+ uv run pyright # strict mode, per-file headers
251
+ uv run tach check # layer boundaries, see tach.toml
252
+ uv run pytest -n auto
253
+ ```
254
+
255
+ Layers, from top to bottom: `cli` → `api` → `pipeline`, `opc`, `pdf` → `features`, `xlsx` → `docx`
256
+ → `markers`, `formatters` → `package`, `ooxml`, `options`. A module may import only lower layers;
257
+ `tach check` enforces it.
258
+
259
+ Security issues: see [SECURITY.md](https://github.com/carbotek/carbotek/blob/main/SECURITY.md).
260
+
261
+ ## License
262
+
263
+ carbotek is licensed under the [PolyForm Noncommercial License 1.0.0](https://github.com/carbotek/carbotek/blob/main/LICENSE).
264
+ Copyright (c) 2026 Carbotek.
265
+
266
+ It is derived from Carbone v3.2.3, Copyright (c) CarboneIO SAS and contributors, licensed under
267
+ the [Apache License 2.0](https://github.com/carbotek/carbotek/blob/main/licenses/Carbone-Apache-2.0.txt). The original code was translated from
268
+ JavaScript to Python and Rust and substantially modified. See [NOTICE](https://github.com/carbotek/carbotek/blob/main/NOTICE) and
269
+ [THIRD_PARTY_LICENSES.md](https://github.com/carbotek/carbotek/blob/main/THIRD_PARTY_LICENSES.md).