How to merge two Word DOCX files together in Python

Merging Docx files in Python just became an absolute snap, as I am about to demonstrate.

For this solution, we will be employing an API that offers the exact functionality that we need. So let’s go ahead and install its client:

pip install cloudmersive-convert-api-client

The function we will need is called merge_document_docx. Below you can see a little block of code for creating its API instance and then the function call from that.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.MergeDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file1 = '/path/to/file' # file | First input file to perform the operation on.input_file2 = '/path/to/file' # file | Second input file to perform the operation on (more than 2 can be supplied).try:# Merge Two Word DOCX Togetherapi_response = api_instance.merge_document_docx(input_file1, input_file2)pprint(api_response)except ApiException as e:print("Exception when calling MergeDocumentApi->merge_document_docx: %s\n" % e)

Easy. Our final step is to simply input our files and send the request. Just like that, we have merged Docx documents. No fuss, no muss.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store