Skip to main content

Compose .docx documents

Project description

docxcompose is a Python library for concatenating/appending Microsoft Word (.docx) files.

Example usage

Append a document to another document:

from docxcompose.composer import Composer
from docx import Document
master = Document("master.docx")
composer = Composer(master)
doc1 = Document("doc1.docx")
composer.append(doc1)
composer.save("combined.docx")

The docxcompose console script

The docxcompose console script allows to compose docx files from the command line, e.g.:

$ docxcompose files/master.docx files/content.docx -o files/composed.docx

Docker container

docxcompose is also available as a Docker container allowing to compose docx documents through a web service.

To start the web service, run:

$ docker run -it --rm -p 8080:8080 4teamwork/docxcompose

To compose documents, just upload them in the desired order as a multipart/form-data request to the web service and you will get back the composed document. Example with curl:

$ curl -F "first=@first.docx" -F "second=@second.docx" -o composed.docx http://localhost:8080/

Installation for development

To install docxcompose for development, clone the repository and using a python with poetry:

$ poetry install

Tests can then be run with pytest.

A note about testing

The tests provide helpers for blackbox testing that can compare whole word files. To do so the following files should be provided:

  • a file for the expected output that should be added to the folder docs/composed_fixture

  • multiple files that can be composed into the file above should be added to the folder docs.

The expected output can now be tested as follows:

def test_example():
    fixture = FixtureDocument("expected.docx")
    composed = ComposedDocument("master.docx", "slave1.docx", "slave2.docx")
    assert fixture == composed

Should the assertion fail the output file will be stored in the folder docs/composed_debug with the filename of the fixture file, expected.docx in case of this example.

Headers and footers

The first document is considered as the main template and headers and footers from the other documents are ignored, so that the header and footer of the first document is used throughout the merged file.

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

docxcompose-2.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

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

docxcompose-2.1.0-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file docxcompose-2.1.0.tar.gz.

File metadata

  • Download URL: docxcompose-2.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for docxcompose-2.1.0.tar.gz
Algorithm Hash digest
SHA256 2b19c3fa0a75905832c6518c302ab3a1fd4d41bf5aa58ad677fc83e4052ad7c0
MD5 8a015e22eb2a720183289246497f94ef
BLAKE2b-256 d3c74c1473b012da40ad58aa67a050e7350e95aa8f18a3e0ffc8b8e8e69ac6c1

See more details on using hashes here.

File details

Details for the file docxcompose-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: docxcompose-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for docxcompose-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8dbff4b7a24349b7766a474dae47930aba39379ba5ac57c6cf8d07c20d67581
MD5 28c3394df5bafaf663a4378e80494457
BLAKE2b-256 895390824863ad89e267aadaef28a3a083b2cc7ddd5a0d8e7981cc89c2342c50

See more details on using hashes here.

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