Compose .docx documents
docxcompose is a Python library for concatenating/appending Microsoft Word (.docx) files.
Append a document to another document:
.. code::
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 allows to compose docx files from the command
line, e.g.:
.. code:: sh
$ docxcompose files/master.docx files/content.docx -o files/composed.docx
To install docxcompose for development, clone the repository and using a python with setuptools (for example a fresh virtualenv), install it using pip:
.. code:: sh
$ pip install -e .[tests]
Tests can then be run with pytest
.
The tests provide helpers for blackbox testing that can compare whole word files. To do so the following files should be provided:
docs/composed_fixture
docs
.The expected output can now be tested as follows:
.. code:: python
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.
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.
Change license from GPL to MIT. [buchi]
Add support for adding, setting and deleting of doc properties. [buchi]
Implement generic handling of referenced parts. Among other, this adds support for embedded Excel charts. [buchi]
Handle embedded SVGs. [buchi]
Add styles from other parts, e.g. footnotes. [buchi]
Fix handling of section type. [buchi]
Fix an issue where the listing style of the first element was different. [deiferni]
Fix issue when restarting intermittent numbering. [deiferni]
Fix error in mapping of num_ids introduced in 1.0.0.a7. [buchi]
Do not fail when numbering zero is referenced. [deiferni]
Fix handling of images in WordprocessingGroups (wpg:wpg). [buchi]
Fix handling of shapes in shape groups (<v:group>). [buchi]
Fix handling of numberings, avoid inserting multiple numbering properties. [buchi]
Fix renumbering of bookmarks. [buchi]
Renumber ids of drawing object properties (wp:docPr). [buchi]
Renumber bookmarks to avoid duplicate ids. [buchi]
Add support for shapes. [buchi]
Fix handling of styles when composing documents with different languages. [buchi]
Also add numberings referenced in styles. [buchi]
Avoid having multiple <w:abstractNum> elements for the same style. [buchi]
Restart first numbering of inserted documents [buchi]
Add support for anchored images. [buchi]
Handle referenced style ids that are not defined in styles.xml [buchi]
Remove header and footer references in paragraph properties. [buchi]
Fix handling of footnotes containing hyperlinks. [buchi]
Add functionality to deal with custom document properties. Properties can be updated and fields containing properties can be removed. When appending or inserting documents their custom document properties get removed automatically. [buchi]