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.1.tar.gz (22.1 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.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docxspec-0.0.1.tar.gz
  • Upload date:
  • Size: 22.1 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.1.tar.gz
Algorithm Hash digest
SHA256 badc9402bd1837108c843243f91d87cc6fa4b19d363a92a92160c66ba8661275
MD5 10fbf90711b5ddf18f555fea0239bcbb
BLAKE2b-256 824dd6e733bf6a9b98754fbb79877a4d0367ce3cb1c2c6bf4d99236860d86bbc

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxspec-0.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: docxspec-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 823329ee163f02f2918ba64cecc7937e9f917763eeac8524572cbdce900def86
MD5 9259aac4c16e69d2de31b2f734c93504
BLAKE2b-256 17a8ebbe69e76c86171d56e1e5ea1bb99a77287800b3a5b8ce0db16adbe31b8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxspec-0.0.1-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