High-level block-based abstraction over python-docx for building dynamic Word documents in code.
Project description
docxblocks
🧱 High-level, block-based abstraction for python-docx.
🚀 Why docxblocks?
Unlike templating libraries like docxtpl, docxblocks keeps all logic in Python, not in .docx files. Build dynamic Word reports from structured block objects inside your codebase.
✨ Key Features
- Block types:
text,heading,table,bullets,image - Style control via consistent
styledictionaries - Graceful fallback for missing data
- Declarative, testable, version-controlled
- No logic inside Word templates
📦 Installation
pip install docxblocks
📘 See the Style Guide for all supported style keys, color formats, and alignment options.
🧱 Block-Based API (Core Concept)
Each piece of content is a block:
{
"type": "text",
"text": "All systems operational.",
"style": {
"bold": True,
"italic": False,
"font_color": "007700",
"align": "center",
"style": "Normal"
}
}
Block types:
| Type | Required Keys |
|---|---|
text |
text |
heading |
text, level |
table |
content |
image |
path |
bullets |
items (list) |
🧪 Example
from docxblocks import DocxBuilder
builder = DocxBuilder("template.docx")
builder.insert("{{main}}", [
{"type": "heading", "text": "Summary", "level": 2},
{"type": "text", "text": "This report provides status."},
{
"type": "table",
"content": {
"headers": ["Service", "Status"],
"rows": [["API", "OK"], ["DB", "OK"]]
},
"style": {
"header_styles": {"bold": True, "bg_color": "f2f2f2"},
"column_widths": [0.5, 0.5]
}
},
{"type": "image", "path": "chart.png", "style": {"max_width": "4in"}}
])
builder.save("output.docx")
🛠️ Philosophy
Keep the logic in your code — not in your Word template.
- Fully programmatic document generation
- No fragile embedded logic (
{{ if x }}) in.docx - Declarative, JSON-like format ideal for automation and templating
- Built for dynamic, testable, repeatable reports
🧪 Development
Setup
# Clone the repository
git clone https://github.com/frank-895/docxblocks.git
cd docxblocks
# Run the development setup script
./scripts/setup_dev.sh
Testing
# Run all tests
PYTHONPATH=. pytest tests
# Run tests with verbose output
PYTHONPATH=. pytest tests -v
# Run specific test file
PYTHONPATH=. pytest tests/test_text_block.py
Examples
# Run individual examples
cd examples
python text_block_example.py
python table_block_example.py
python image_block_example.py
python combined_example.py
Continuous Integration
GitHub Actions automatically runs tests on:
- Every push to
mainanddevelopbranches - Every pull request to
main - Multiple Python versions (3.9, 3.10, 3.11)
Note: Tests run automatically in CI, so you can push your changes and see the results on GitHub.
📄 License
MIT - LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docxblocks-1.0.2.tar.gz.
File metadata
- Download URL: docxblocks-1.0.2.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44b0b8293395307863cca1b4f87cdea3a486b471527adaabe1d7890aeb41b5cd
|
|
| MD5 |
134c76e87f6e897d58836e82584f1586
|
|
| BLAKE2b-256 |
46f969acd4bc940f5e383142588ff4bce1ddfbfb7635b68493a34a6107d2a678
|
Provenance
The following attestation bundles were made for docxblocks-1.0.2.tar.gz:
Publisher:
release.yml on frank-895/docxblocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docxblocks-1.0.2.tar.gz -
Subject digest:
44b0b8293395307863cca1b4f87cdea3a486b471527adaabe1d7890aeb41b5cd - Sigstore transparency entry: 250090227
- Sigstore integration time:
-
Permalink:
frank-895/docxblocks@2a1bc26c6857fc010d52d348207a76ed46ea4a73 -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/frank-895
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a1bc26c6857fc010d52d348207a76ed46ea4a73 -
Trigger Event:
push
-
Statement type:
File details
Details for the file docxblocks-1.0.2-py3-none-any.whl.
File metadata
- Download URL: docxblocks-1.0.2-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63a50145240295764867d62c3ee6b7e2a135568c4f3c805f50ef93932ebb6f1c
|
|
| MD5 |
a04d4845bc859e21acde15289cbbf54d
|
|
| BLAKE2b-256 |
5fb79586ba37d2a7812118ef5c044d3e2ed4be7dcc31f3f449b9d1ae5fbca3d9
|
Provenance
The following attestation bundles were made for docxblocks-1.0.2-py3-none-any.whl:
Publisher:
release.yml on frank-895/docxblocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docxblocks-1.0.2-py3-none-any.whl -
Subject digest:
63a50145240295764867d62c3ee6b7e2a135568c4f3c805f50ef93932ebb6f1c - Sigstore transparency entry: 250090248
- Sigstore integration time:
-
Permalink:
frank-895/docxblocks@2a1bc26c6857fc010d52d348207a76ed46ea4a73 -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/frank-895
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a1bc26c6857fc010d52d348207a76ed46ea4a73 -
Trigger Event:
push
-
Statement type: