This article provides you with the easiest solution of how to merge multiple Word documents in Python in a few simple steps.
There could be various scenarios when you have to combine multiple Word documents into a single file. For example, when multiple persons are writing different sections of a document and you need to combine the content at the end. On the other hand, you may need to merge all the invoices into one Word document. So let’s see how to combine multiple Word documents in Python.
Python Library to Merge Word Documents
To combine the DOCX or DOC files, we will use Aspose.Words for Python, which is a powerful library to create and manipulate MS Word files. It also allows you to split, combine, and convert Word documents seamlessly. You can install the library from PyPI using the following pip command.
pip install aspose-words
Combine Multiple Word DOCX Files in Python
While combining the Word documents, you can specify the formatting you want to apply to the source document. For example, when combining document B with A, you can change the formatting of B as A or keep its original formatting as it is. The following are the steps to combine two Word documents in Python.
- Load the source Word document using Document class.
- Load the destination Word document using Document class.
- Combine documents by appending source document into destination document using Document.append_document(Document, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) method.
- Select the ImportFormatMode as per your requirements.
- Save the destination document using Document.save(string) method.
The following code sample shows how to combine two Word DOCX files in Python.
Source Document
The following is the screenshot of the source Word document we have used in this article.
Destination Document
The following screenshot shows the destination Word document.
Combined Document
The following is the screenshot after combining both Word documents.
Merge Word Documents with a Free License
Get a free temporary license and merge Word documents without evaluation limitations.
Conclusion
In this article, you have learned how to combine two Word documents in a single document using Python. You can install Aspose.Words for Python in your applications and integrate the provided code to merge Word documents. In addition to this, you can explore Aspose.Words for Python using the documentation. In case you would have any queries, you can ask us via our forum.