How to compare two Microsoft Word DOCX Files in Python

Cloudmersive
2 min readApr 27, 2020

It’s really quite tedious to set up a comparison function for two or more DOCX files. I should know, I’ve already done it. Luckily for you, however, you won’t have to, as I have also packaged my solution into a handy little API. Let me show you how to use it.

Starting things off, use pip install for our client installation.

pip install cloudmersive-convert-api-client

And now below I have set up your function call code for compare_document_docx, voila:

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.CompareDocumentApi(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:# Compare Two Word DOCXapi_response = api_instance.compare_document_docx(input_file1, input_file2)pprint(api_response)except ApiException as e:print("Exception when calling CompareDocumentApi->compare_document_docx: %s\n" % e)

Ok so now we just need our two (or more) DOCX inputs. Once it has those, the API will perform our comparison remotely, then return the result, with highlighted differences. What more could you ask for!

--

--

Cloudmersive

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