Merge Word documents online. Combine DOCX using Python

Merge Word files in Python

In large distributed teams, multiple team members work on different modules and produce related artifacts. However, for an end-user, a single combined document is required in a logical manner. The process of manually combining the documents can be adopted for a small set of documents but it gets really cumbersome when the document pool keeps on increasing or you stumble across a requirement to generate multiple versions of the document with a specific set of files. In that scenario, a programmatic approach is a viable solution. We are further going to discuss the details on how to combine multiple Word documents into a single merged file in Python.

Python programming SDK

Aspose.Words Cloud Python SDK is a programming SDK for Python developers to create as well as manipulate MS Word (DOCX, DOC, DOT, RTF, DOCM) and OpenDocument (ODT, OTT), etc formats. It is based on Aspose.Words Cloud REST API and all the features offered by the API can be easily utilized within Python code. For related details, please visit Supported File Formats.

Installation of SDK

In order to use the SDK, the first step is to install it on the local system. The Python programming SDK is available for download at PIP and GitHub. Execute the following command on the command line terminal to install the SDK

pip install aspose-words-cloud

In case you are using PyCharm IDE, simply search asposewordscloud on Python Interpreter window under PyCharm -> Preferences… section.

Aspose.Words Cloud Python SDK installation

Combine Word files using the cURL command

The cURL commands are one of the easiest ways to access REST APIs across the platform without worrying about the underlying architecture. Since Aspose.Words Cloud is developed according to REST architecture, so we can also access the API via cURL commands.

However, in order to use the cURL commands, the first step is to generate JSON Web Token (JWT) which is used to access the APIs through cURL commands. Before proceeding further, the first step is to create an account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details. For more information, please visit How to Obtain JWT token using a Client ID and Client Secret key.

Use the following command to generate JWT token based on provided Client ID and Client Secret.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4&client_secret=d87269aade6a46cdc295b711e26809af" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT Token, we can run following command to merge two MS Word files already available in cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/ComparisonResult.docx/appendDocument?destFileName=MergedFile.docx" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"DocumentEntries\":[{\"Href\":\"SampleMailMergeTemplate.docx\",\"ImportFormatMode\":\"KeepSourceFormatting\"}],\"ApplyBaseDocumentHeadersAndFootersToAppendingDocuments\":true}"
  • ComparisonResult.docx – is the source document
  • MergedFile.docx – resultant output document
  • SampleMailMergeTemplate.docx – file to be appended

Merge DOCX files in Python

Please follow the instructions and steps given below to upload two Word files to Cloud storage and then merge MS Word documents in Python.

  • The first step is to create an object of WordsApi class while passing Client ID and Client Secret details as arguments
  • Secondly, call the upload_file(…) method of WordsApi class to upload the document to Cloud storage
  • Now create an object of DocumentEntry(…) which takes the document to be merged and ImportFormatMode value as arguments
  • Then create an object of DocumentEntryList(…) which takes the DocumentEntry object created above as argument
  • Finally, call the AppendDocumentRequest API to merge the documents and save the output on Cloud storage

Conclusion

In this article, we have discussed the process of merging Word documents together using Python REST API. The Python SDK is open source and its complete code snippet can be downloaded from GitHub. Other than using API through the cURL command and Python programming language, you can also access it via the swagger interface.

We also recommend going through the following blog posts