How to merge odt files in Python

286 Views Asked by At

I have several odt files, and I would like to merge them into a new one.

I am using relatorio library to read the odt files.

from relatorio.templates.opendocument import Template
from os.path import dirname, join

odt_1 = Template(source='', filepath='report_test_1_fulled.odt')
odt_2 = Template(source='', filepath='report_test_2_fulled.odt')

In order to merge them into one I have tried two options without success:

  1. Use Aspose library to convert them to .docx and then do the merge. But when installing the library pip install aspose-words I get this error:
ERROR: Could not find a version that satisfies the requirement aspose-words (from versions: none)
ERROR: No matching distribution found for aspose-words

I think that this might be because my computer operative system is macOS Catalina, and this does not fullfil the requirements.

  1. Merge both odt files, but I cannot figure out how.

¿Any suggestion? The final goal is to end up with one .odt or .docx containing both.

1

There are 1 best solutions below

1
Alexey Noskov On

To combine two documents using Aspose.Words you can use Document.append_document method.

dstDoc = aw.Document("documentA.odt")
srcDoc = aw.Document("documentB.odt")

# Append the source document to the destination document.
# Pass format mode to retain the original formatting of the source document when importing it.
dstDoc.append_document(srcDoc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)

dstDoc.save("out.odt")

You can learn more about appending and inserting documents here: https://docs.aspose.com/words/python-net/insert-and-append-documents/

PS: You are right, currently Aspose.Words for Python does not support MacOS.