merge word documents

How to Merge Word Documents online

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 manual approach to merge documents may get cumbersome when the document pool keeps increasing. The same would be the case when we need to generate multiple versions of the document with a selected set of files. So a programmatic approach is a viable solution. In this article, we are going to discuss the details of how to merge word Documents in Python.

Merge Documents API

Aspose.Words Cloud Python SDK is a programming API offering the capabilities to create as well as manipulate MS Word (DOCX, DOC, RTF) and OpenDocument (ODT, OTT), formats. Now in order to use the SDK, please 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 the Python Interpreter window under PyCharm -> Preferences… section.

Before proceeding further, we also need to create a free account by visiting Aspose.Cloud dashboard. Simply Sign Up using GitHub or Google account or click the Create a new Account button. Please note your Client Credentials as we are going to use them in the following sections.

Merge Word Documents in Python

Please follow the instructions given below to upload two Word files to Cloud storage and then merge them using a Python code snippet.

  • 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 an argument
  • Finally, call the AppendDocumentRequest API to merge the documents and save the output on Cloud storage

Combine Word Documents using cURL Commands

The cURL commands are one of the easiest ways to access REST APIs across the platform. So we are going to access the API via cURL commands. However, in order to use the cURL commands, the first step is to generate JSON Web Token (JWT). Please use the following command to generate a JWT token based on provided Client ID and Client Secret details.

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"

For more information, please visit How to Obtain JWT token using a Client ID and Client Secret key.

Once we have the JWT Token, please execute the following command to merge documents 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

Conclusion

In this article, we have discussed the details of how to merge word document using Python code snippets. Please note that the complete source code of SDK can be downloaded from GitHub. Furthermore, in order to explore the API capabilities, you may consider accessing it via the swagger interface.

We highly recommend going through the following blog posts