DOCX Compare - Python Sample Code

Requirements

This is sample code for using Apryse SDK to compare two DOCX documents and produce an output DOCX document where the differences are recorded as tracked changes. The comparison runs entirely within the SDK with no external or system dependencies, producing identical results across Windows, Linux, macOS, and Android. Sample code is provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Python, Ruby, Go, Objective-C, and VB.

Implementation steps

To compare DOCX files with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework.
Step 2: Add the sample code provided in this guide.

Learn more about Apryse Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing the test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18# Provide your own original and revised versions of a DOCX document here.
19original_filename = "SYH_Letter.docx"
20revised_filename = "SYH_Letter_revision2.docx"
21output_filename = "SYH_Letter_changes.docx"
22
23#---------------------------------------------------------------------------------------
24# The following sample illustrates how to use the Office.DOCXCompare utility class to
25# compare two MS Word (DOCX) documents and produce a new DOCX document containing the
26# differences between them as tracked changes.
27#
28# This comparison is performed entirely within the PDFNet and has *no* external or
29# system dependencies -- Comparison results will be the same whether on Windows,
30# Linux or Android.
31#
32# Please contact us if you have any questions.
33#---------------------------------------------------------------------------------------
34
35def main():
36 # The first step in every application using PDFNet is to initialize the
37 # library. The library is usually initialized only once, but calling
38 # Initialize() multiple times is also fine.
39 PDFNet.Initialize(LicenseKey)
40 PDFNet.SetResourcesPath("../../../Resources")
41
42 try:
43 options = DOCXCompareOptions()
44
45 # Compare the two DOCX documents, writing the differences as tracked
46 # changes into the output DOCX document.
47 result = DOCXCompare.Compare(input_path + original_filename, input_path + revised_filename, output_path + output_filename, options)
48
49 # And we're done!
50 if result.DifferencesDetected():
51 print("Differences detected, saved to " + output_filename)
52 else:
53 print("No difference detected")
54 except Exception as e:
55 print("Unable to compare DOCX documents, error: " + str(e))
56
57 PDFNet.Terminate()
58 print("Done.")
59
60if __name__ == '__main__':
61 main()
62
63

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales