Merge Multiple Word Documents into Single Doc using REST API in Python


/ Published in: Python
Save to your folder(s)

This REST example uses pycurl library to send HTTP request and handle HTTP response so you need to install it to use this example. Once input Word documents are uploaded, you can use the following URI to merge documents on Aspose for 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. All or specific parameters can be used according to your requirement.

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

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


Copy this code and paste it in your HTML
  1. ####### Section 1 ######
  2. # specify App SID
  3. AsposeApp.app_sid = '77****-****-****-****-80*********'
  4. # specify App Key
  5. AsposeApp.app_key = '******************'
  6. #build URI to merge PDFs
  7. str_uri = 'http://api.aspose.com/v1.1/words/MainDocument.docx/appendDocument'
  8. #sign URI
  9. signed_uri = Utils.sign(Utils(), str_uri)
  10.  
  11. ####### End Section 1 ######
  12.  
  13. ####### Section 2 ######
  14. #Build JSON to post
  15. json_document1 = json.dumps({'Href' : 'AppendDocument1.docx', 'ImportFormatMode' : 'KeepSourceFormatting'})
  16. json_document2 = json.dumps({'Href' : 'AppendDocument2.docx', 'ImportFormatMode' : 'UseDestinationStyles'})
  17. json_arr = '{\'DocumentEntries\':[' + json_document1 + ',' + json_document2 + ']}'
  18.  
  19. ####### End Section 2 ######
  20.  
  21. ####### Section 3 ######
  22. Utils.process_command(Utils(), signed_uri, 'POST', 'JSON', json_arr)
  23. ####### End Section 3 ######
  24.  
  25. ####### Section 4 ######
  26.  
  27. #build URI to download output file
  28. str_uri = 'http://api.aspose.com/v1.1/storage/file/MainDocument.docx'
  29. #sign URI
  30. signed_uri = Utils.sign(Utils(), str_uri)
  31. response_stream = Utils.process_command(Utils(),signed_uri, 'GET')
  32. Utils.save_file(Utils(),response_stream, 'MergedFile.docx')
  33.  
  34. ####### End Section 4 ######

URL: http://www.aspose.com/blogs/aspose-products/aspose-words-product-family/archive/2014/03/18/merge-multiple-word-documents-into-one-document-in-python.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.