Skip to main content

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 style dictionaries
  • 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 main and develop branches
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docxblocks-1.0.1.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxblocks-1.0.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file docxblocks-1.0.1.tar.gz.

File metadata

  • Download URL: docxblocks-1.0.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for docxblocks-1.0.1.tar.gz
Algorithm Hash digest
SHA256 754756615c596b8f93ae10fc8596f9d1011b583b21ff6d8dd3b240763dca8176
MD5 6ffec0bbf1cd17ead4ee0989958ff13b
BLAKE2b-256 a4e69287fd8d64b3bab78dccaca96fb3cb3246f2deb9654c117fffc347bc3a9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxblocks-1.0.1.tar.gz:

Publisher: release.yml on frank-895/docxblocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file docxblocks-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: docxblocks-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for docxblocks-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82bc9cee3f29b391fc2009e09edf8011ef1f885a4cc8996009312935c8991154
MD5 b80aabb4afd1b5665e746fba833f09d4
BLAKE2b-256 1c89a832e28d431a7ff26abf7d335ce01818ad9245a950b16b97bf6c6e4aa4f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxblocks-1.0.1-py3-none-any.whl:

Publisher: release.yml on frank-895/docxblocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page