Merge multiple word documents If you are building applications to generate or process Microsoft Word documents, merging multiple documents into one document is an important feature.

Aspose.Words Cloud supports DOC, OOXML, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats and enables you to perform a wide range of document processing tasks in any language including .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. You can use it with any language or platform that supports REST. (Almost all platforms and languages support REST and provide native REST clients to work with REST APIs.)

This post explains how to merge multiple Word documents into a single document in Python. You can check the Append a List of Word Documents topic for other languages.

To merge documents, you need to upload the input Word documents to Aspose Cloud or any supported third party storage and then send a POST request to the Aspose Cloud service. The steps below describe the process in detail.

Merge Multiple Word Documents using Python REST

This REST example uses the pycurl library to send HTTP requests and handle HTTP responses so you need to install it to use this example.

Once the input Word documents are uploaded, use the following URI to merge documents on Aspose Cloud or any supported third party storage.

http://api.aspose.com/v1.1/words/MainDocument.docx/appendDocument

You can use a couple of optional parameters with the above mentioned URI. Pick the parameters you want, or use all.

  • storage – Used to set the storage name if you are using third-party storage.
  • folder – Used to set the name and path of the folder the input files have been uploaded to.

After building the URI, you need to go through the following steps:

  1. Set App SID and App Key and sign the URI.
    See section 1 of code below and Sign URI method for more details.
  2. Build JSON to post in the request body.
    A list of input documents including paths and import format modes should be provided. See section 2 of the code below.
  3. Send a POST request to the Aspose Cloud service.
    See section 3 of the code below and process_command method for more details.
  4. Download the merged document file if required.
    See section 4 of the code below.

Following is the code to merge multiple Word documents.

####### Section 1 ######

sepcify App SID

AsposeApp.app_sid = ‘77****-****-****-****-80*********’

sepcify App Key

AsposeApp.app_key = ‘******************’

#build URI to merge PDFs
str_uri = ‘http://api.aspose.com/v1.1/words/MainDocument.docx/appendDocument'
#sign URI
signed_uri = Utils.sign(Utils(), str_uri)

####### End Section 1 ######

####### Section 2 ######
#Build JSON to post
json_document1 = json.dumps({‘Href’ : ‘AppendDocument1.docx’, ‘ImportFormatMode’ : ‘KeepSourceFormatting’})
json_document2 = json.dumps({‘Href’ : ‘AppendDocument2.docx’, ‘ImportFormatMode’ : ‘UseDestinationStyles’})
json_arr = ‘{‘DocumentEntries’:[’ + json_document1 + ‘,’ + json_document2 + ‘]}’
####### End Section 2 ######

####### Section 3 ######
Utils.process_command(Utils(), signed_uri, ‘POST’, ‘JSON’, json_arr)
####### End Section 3 ######

####### Section 4 ######
#build URI to download output file
str_uri = ‘http://api.aspose.com/v1.1/storage/file/MainDocument.docx'
#sign URI
signed_uri = Utils.sign(Utils(), str_uri)
response_stream = Utils.process_command(Utils(),signed_uri, ‘GET’)
Utils.save_file(Utils(),response_stream, ‘MergedFile.docx’)
####### End Section 4 ######

Merge Multiple Word Documents using Python SDK

If you want to use our Python SDK to merge Word documents, you can download this SDK from Aspose Cloud SDK for Python. To use the Python SDK, you need to:

  1. Set base product URI, App SID, App Key and output location. See section 1 of the complete code.
  2. Create object of WordDocument class and call append_document method passing array of names of documents to merge, array of import format modes, and folder name where input documents are present. See section 3 of the complete code.

Following is the complete code:

import os
import asposestoragecloud
import asposewordscloud
import asposewordscloud.models.requests

class Document(object):

	def __init__(self):
		api_client = asposewordscloud.ApiClient()
		api_client.configuration.host = 'https://api.aspose.cloud'
		api_client.configuration.api_key['api_key'] = 'xxxx' # Put your appkey here
		api_client.configuration.api_key['app_sid'] = 'xxxx-xxxx-xxxx-xxxx-xxxx' # Put your appSid here

		# Same credentials for storage
		self.storage_api = asposestoragecloud.StorageApi(asposestoragecloud.ApiClient(apiKey='xxxx', appSid='xxxx-xxxx-xxxx-xxxx-xxxx')) # Same credentials for storage
		self.storage_api.api_client.configuration.base_url = 'https://api.aspose.cloud' + '/v1.1'
		self.words_api = asposewordscloud.WordsApi(api_client)

		ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)) + "/../..")
		self.dataFolder = os.path.join(ABSPATH, "TestData/Common")

	def appendDocument(self):
		main_document = 'test_multi_pages.docx'
		append_document_1 = 'test_doc.docx'
		append_document_2 = 'TableDocument.doc'
		dest_name = 'FinalDocument.docx'
		
		# Upload Main Document and "Documents to append" to Cloud Storage
		for filename in [main_document, append_document_1, append_document_2]:
			filePath = os.path.join(self.dataFolder, filename)
			with open(filePath, 'rb') as f:
				fileData = f.read()
			file_upload_response = self.storage_api.put_create(filename, fileData)
		
		# importFormatMode defines which formatting will be used: appended or destination document. 
		# Can be KeepSourceFormatting or UseDestinationStyles. 
		doc_entry_1 = asposewordscloud.DocumentEntry(append_document_1,
                                             'KeepSourceFormatting')
		doc_entry_2 = asposewordscloud.DocumentEntry(append_document_2,
                                             'UseDestinationStyles')
		document_list = asposewordscloud.DocumentEntryList([doc_entry_1, doc_entry_2])
		request = asposewordscloud.models.requests.PostAppendDocumentRequest(main_document, 
																			document_list,
                                                                       dest_file_name=dest_name)
		result = self.words_api.post_append_document(request)
		print("Result {}".format(result))

document = Document()
document.appendDocument()

Suggested Article:

Create, Edit, Merge or Convert a Word Document without MS Word