DocxBlocks
A Python library for building Word documents using a block-based API. Create complex documents with simple, readable code.
🚀 Quick Start
from docxblocks import DocxBuilder
# Create blocks
blocks = [
{"type": "heading", "text": "Report Title", "level": 1},
{"type": "text", "text": "This is the first paragraph."},
{"type": "text", "text": "This continues inline."},
{"type": "text", "text": "\nThis starts a new paragraph."},
{"type": "table", "content": {
"headers": ["Name", "Value"],
"rows": [["Item 1", "100"], ["Item 2", "200"]]
}}
]
# Build document
builder = DocxBuilder("template.docx")
builder.insert("{{content}}", blocks)
builder.save("output.docx")
📋 Features
- Block-based API: Each piece of content is a simple dictionary
- Smart text grouping: Consecutive text blocks are grouped inline
- Simple newlines: Every
\ncreates a new paragraph - Rich styling: Bold, italic, colors, alignment, and more
- Table support: Headers, rows, column widths, cell styling
- Image handling: Automatic sizing with DPI calculation
- Template support: Use existing Word templates as starting points
- Error handling: Graceful fallbacks for missing content
📖 Installation
pip install docxblocks
🎯 Basic Usage
Template Setup
Create a Word document with placeholders:
{{header}}
{{main}}
{{footer}}
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"
}
}
Text Block Behavior
Text blocks have simple, predictable behavior:
- Every
\n: Always starts a new paragraph - Inline grouping: Consecutive text blocks without
\nare grouped together spacing: Adds extra blank paragraphs after the block
{"type": "text", "text": "Line 1\nLine 2\nLine 3"}
# Renders as three paragraphs: Line 1, Line 2, Line 3
{"type": "text", "text": "First paragraph\n\nSecond paragraph"}
# Renders as: First paragraph, [blank paragraph], Second paragraph
{"type": "text", "text": "First", "spacing": 1}
# Renders as a new paragraph, then one blank paragraph after
Table Cell Behavior
Table cells and headers follow the same rules:
- Every
\n: Always starts a new paragraph within the cell - Inline grouping: Consecutive cell blocks without
\nare grouped together
{
"type": "table",
"content": {
"headers": ["Name", "Description\nDetails"],
"rows": [
["Item 1", "First paragraph.\nSecond paragraph."],
["Item 2", "Line 1\n\nLine 2 with empty line above"]
]
}
}
# Each '\n' creates a new paragraph, each '\n\n' creates paragraph with empty line before
Block types:
| Type | Required Keys | Optional Keys |
|---|---|---|
text |
text |
style, spacing |
heading |
text, level |
style |
bullets |
items |
style |
table |
content |
style |
image |
path |
style |
page_break |
- | - |
header |
content |
apply_to |
footer |
content |
apply_to |
🎨 Styling
Text Styling
{
"type": "text",
"text": "Styled text",
"style": {
"bold": True,
"italic": False,
"font_color": "FF0000", # Red
"align": "center", # "left", "center", "right", "justify"
"style": "Normal"
}
}
Alignment Options
All block types support four alignment options:
# Text alignment examples
{"type": "text", "text": "Left aligned", "style": {"align": "left"}}
{"type": "text", "text": "Centered text", "style": {"align": "center"}}
{"type": "text", "text": "Right aligned", "style": {"align": "right"}}
{"type": "text", "text": "Justified text that adjusts spacing for clean edges", "style": {"align": "justify"}}
# Works with all block types
{"type": "heading", "text": "Centered Heading", "level": 1, "style": {"align": "center"}}
{"type": "bullets", "items": ["Item 1", "Item 2"], "style": {"align": "right"}}
Table Styling
{
"type": "table",
"content": {"headers": ["A", "B"], "rows": [["1", "2"]]},
"style": {
"column_widths": [0.3, 0.7],
"header_styles": {"bold": True, "bg_color": "f2f2f2"},
"column_styles": {0: {"font_color": "FF0000"}}
}
}
Rich Content in Cells: Table cells can contain text, images, bullets, and headings. See STYLEGUIDE.md for examples.
Image Styling
{
"type": "image",
"path": "logo.png",
"style": {
"max_width": "4in",
"max_height": "300px",
"wrap_text": "square",
"horizontal_align": "left",
"vertical_align": "top",
"distance_from_text": "0.1in"
}
}
Text Wrapping Options: inline, square, tight, through, top_and_bottom, behind, in_front
Positioning: horizontal_align (left/center/right), vertical_align (top/middle/bottom), distance_from_text
See STYLEGUIDE.md for comprehensive styling examples.
📄 Headers and Footers
DocxBlocks supports customizable headers and footers that can contain any combination of supported block types.
Basic Usage
blocks = [
{
"type": "header",
"apply_to": "all",
"content": [
{
"type": "text",
"text": "Company Name\tDocument Title\tPage {{page}}",
"style": {"align": "center", "font_color": "666666"}
}
]
},
{
"type": "footer",
"apply_to": "all_except_first",
"content": [
{
"type": "text",
"text": "© 2024 Company. All rights reserved.",
"style": {"align": "center", "italic": True}
}
]
}
]
Apply To Options: "all", "all_except_first", "first", "odd", "even"
See STYLEGUIDE.md for advanced examples including images and tables in headers/footers.
🔧 Advanced Features
Mixed Content
blocks = [
{"type": "heading", "text": "Section 1", "level": 2},
{"type": "text", "text": "Introduction "},
{"type": "text", "text": "with ", "style": {"bold": True}},
{"type": "text", "text": "inline styling."},
{"type": "bullets", "items": ["Point 1", "Point 2"]},
{"type": "table", "content": {...}},
{"type": "page_break"},
{"type": "heading", "text": "Section 2", "level": 2}
]
Template Variables
# Template.docx contains: "Hello {{name}}, here is your {{report_type}} report."
builder = DocxBuilder("template.docx")
builder.insert("{{name}}", [{"type": "text", "text": "John"}])
builder.insert("{{report_type}}", [{"type": "text", "text": "monthly"}])
builder.save("output.docx")
📚 Examples
See the examples/ directory for complete working examples:
text_block_example.py- Basic text blocks and stylingtable_block_example.py- Table creation and stylingheader_footer_example.py- Headers and footers with various configurationsall_except_first_example.py- Cover page example with clean first page and headers on content pagesnewline_example.py- Newline handling and paragraph behaviorinline_text_example.py- Inline text groupingalignment_example.py- Text alignment with left, center, right, and justifycombined_example.py- Mixed block types
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
📄 License
MIT License - see LICENSE file for details.
🐛 Issues
Found a bug? Have a feature request? Please open an issue on GitHub.
Metadata
Release files for docxblocks 1.6.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| docxblocks-1.6.6.tar.gz | 33.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| docxblocks-1.6.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 60.9 kB
Release files / docxblocks-1.6.6.tar.gz
| Download URL | docxblocks-1.6.6.tar.gz |
|---|---|
| Size | 33.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
460716c074acea0011ba552c54c48190f1743f33526207ddcb9ce1332557f292
|
|
BLAKE2b-256 checksum How to use checksums |
f22eea956dd1f22e60398af10ea8404e29a328a1938fbb7b9347cce0fd37e675
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jun 29, 2025.
Transparency logRelease files / docxblocks-1.6.6-py3-none-any.whl
| Download URL | docxblocks-1.6.6-py3-none-any.whl |
|---|---|
| Size | 27.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
13290598f5be5f47b8c5473d814ba2a5e650cccca80ee16667fd8d6702cf3d05
|
|
BLAKE2b-256 checksum How to use checksums |
73871baa5f00bbf15445fdb80390161824c4398ba0057cedc04b218d54a40b69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jun 29, 2025.
Transparency log