Skip to main content

Lightweight Word report generation library based on python-docx

Project description

docxspec

English | 简体中文

PyPI Python License CI Publish

docxspec is a lightweight Word report generation library built on top of python-docx.

It provides a small, structured API for generating .docx reports from user-supplied templates and containerized content blocks. It is suitable for automated test reports, simulation reports, and other document-generation workflows.

Features

  • Template-based Word report generation
  • Structured content container API
  • Text, image, and table insertion
  • Rich text style helpers
  • Automatic figure and table caption numbering
  • PyPI-ready packaging and CI/CD workflows

Installation

Install from PyPI:

pip install docxspec

Or install from source:

git clone https://github.com/Poggi-Tang/docxspec.git
cd docxspec
pip install -e .

Quick Start

from docxspec import BODY_STYLE, WordAPI, make_rich_text

api = WordAPI("your_template.docx")

text = make_rich_text(
    "This text is inserted into the template.",
    BODY_STYLE,
)

image_container = api.new_container()
image_container.add_image(
    "your_image.png",
    width_cm=8.0,
    align="center",
)

table_container = api.new_container()
table_container.add_table_by_config(
    {
        "data": [
            ["Name", "Value"],
            ["Example", "123"],
        ]
    }
)

api.render(
    {
        "text": text,
        "image": image_container.subdoc,
        "table": table_container.subdoc,
    },
    "report.docx",
)

Demo Directory

The repository includes a runnable demo set in Demo/.

It uses one shared template file:

  • Demo/template.docx

And provides these example scripts:

  • Demo/demo1_paragraph.py
  • Demo/demo2_container_paragraph.py
  • Demo/demo3_container_image_caption.py
  • Demo/demo4_container_table_caption.py
  • Demo/demo5_container_table_image_caption.py
  • Demo/demo6_header_footer.py
  • Demo/demo7_styles_in_container.py
  • Demo/demo8_all_in_one.py

Run them from the repository root, for example:

python Demo/demo1_paragraph.py
python Demo/demo8_all_in_one.py

Generated files are written to Demo/output/.

Note: template and demo assets are repository examples. They are not packaged into the published wheel.

Project Structure

docxspec
├── .github/
│   └── workflows/
├── Demo/
├── src/
│   └── docxspec/
│       ├── __init__.py
│       ├── word_api.py
│       └── word_styles.py
├── tests/
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── README.zh-CN.md
└── pyproject.toml

Release Automation

This repository is prepared for a professional Python package workflow:

  • CI runs lint and tests on push and pull request.
  • Semantic Release updates the version, changelog, tag, and GitHub Release.
  • Trusted Publishing publishes to PyPI from GitHub Actions without a PyPI API token.
  • Build artifacts include both source distribution and wheel.

Development

pip install -e .[dev]
pytest
ruff check .

When developing locally, prepare your own .docx template files in the repository or project workspace and pass their paths explicitly to WordAPI.

License

MIT License. See 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

docxspec-0.0.3.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

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

docxspec-0.0.3-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file docxspec-0.0.3.tar.gz.

File metadata

  • Download URL: docxspec-0.0.3.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for docxspec-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ce83219e424153a95a8da9054b2a4a21ed783e3ff55c9b5008d786085acf298e
MD5 75b18acce006f5e2c23eabd187f99195
BLAKE2b-256 636618ae0dac7ab814bcff55f56f755c6039ef2a701782df13585d773d66e6c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxspec-0.0.3.tar.gz:

Publisher: publish.yml on Poggi-Tang/DocxSpec

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

File details

Details for the file docxspec-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: docxspec-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for docxspec-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e2ff719ad7eea4667c598a176da9bce26afb7035f24dc77003f1d90a8481fcd3
MD5 55f04d7849c346bc9756fbc77e2d0b4f
BLAKE2b-256 5476f46c5022de71b0bbd3f970b899083d4833c130baf809a2be0d650c7a018a

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxspec-0.0.3-py3-none-any.whl:

Publisher: publish.yml on Poggi-Tang/DocxSpec

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