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.
Release files for docxcompose 2.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| docxcompose-2.2.0.tar.gz | 22.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| docxcompose-2.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 47.8 kB
Release files / docxcompose-2.2.0.tar.gz
| Download URL | docxcompose-2.2.0.tar.gz |
|---|---|
| Size | 22.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2c69703a2fefad4471aad82861a1c5d7b3f4a669510de504bb36f41f66f6d3e
|
|
BLAKE2b-256 checksum How to use checksums |
0d10d0941047b177c0b6eb95138b15949ca59a8ddb1d7266d10dc384d0dba681
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / docxcompose-2.2.0-py3-none-any.whl
| Download URL | docxcompose-2.2.0-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fcfec8b0ba7d341bd3280cb92b571ed2f9e868bbd0d6750ea9539e0ab0bb49a1
|
|
BLAKE2b-256 checksum How to use checksums |
e470121090145cb543e48da7421e64036e6484bfbbc40fc619d73599dc22eb2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|