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/

Options

Preserving styles

By default docxcompose tries to apply styles from the first document to the appended documents. This should ensure a consistent appearance throughout the composed document.

With the preserve-styles option, it’s possible to change this behavior to keep the styles of the appended documents.

The option can be given through the command line (--preserve-styles), via the web service url using an url parameter (preserve_styles=1) or using Python Composer(preserve_styles=True).

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.2.0.tar.gz (22.0 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.2.0-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for docxcompose-2.2.0.tar.gz
Algorithm Hash digest
SHA256 e2c69703a2fefad4471aad82861a1c5d7b3f4a669510de504bb36f41f66f6d3e
MD5 09498990386dec3386aed64770772729
BLAKE2b-256 0d10d0941047b177c0b6eb95138b15949ca59a8ddb1d7266d10dc384d0dba681

See more details on using hashes here.

File details

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

File metadata

  • Download URL: docxcompose-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fcfec8b0ba7d341bd3280cb92b571ed2f9e868bbd0d6750ea9539e0ab0bb49a1
MD5 80249184b16f809c9f09218398dbff58
BLAKE2b-256 e470121090145cb543e48da7421e64036e6484bfbbc40fc619d73599dc22eb2e

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