Skip to main content

Block-based Word document builder with simple, predictable newline and inline text handling.

Project description

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 \n creates 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 \n are 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 \n are 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 - -

🎨 Styling

Text Styling

{
    "type": "text",
    "text": "Styled text",
    "style": {
        "bold": True,
        "italic": False,
        "font_color": "FF0000",  # Red
        "align": "center",
        "style": "Normal"
    }
}

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"}}
    }
}

Image Styling

{
    "type": "image",
    "path": "logo.png",
    "style": {
        "max_width": "4in",
        "max_height": "300px"
    }
}

🔧 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 styling
  • table_block_example.py - Table creation and styling
  • newline_example.py - Newline handling and paragraph behavior
  • inline_text_example.py - Inline text grouping
  • combined_example.py - Mixed block types

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. 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.

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.3.7.tar.gz (20.2 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.3.7-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for docxblocks-1.3.7.tar.gz
Algorithm Hash digest
SHA256 1e6f110865ac9580b90458cb7edfaebff5d5e20893434c5061572808f906d0e3
MD5 58a5ae01d3f4af66a35c09a08b65a140
BLAKE2b-256 f05ad3477d51e5ab3228446a90849605f8bfdea06b1a787c684a7509ecfbf7c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxblocks-1.3.7.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.3.7-py3-none-any.whl.

File metadata

  • Download URL: docxblocks-1.3.7-py3-none-any.whl
  • Upload date:
  • Size: 19.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.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7df254c54731f1a0178a1c8bf66173253ee4bf8142b75725e0c27bf14844c797
MD5 9ff194606a447aee1a926e66488188e0
BLAKE2b-256 5241d95543df1299e78a7583f085034515cac8d835cd64838b9fe2b949962690

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxblocks-1.3.7-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