DEV Community

Jeremy K.
Jeremy K.

Posted on

Compare Two Word Documents Using Python

In everyday office workflows—whether reviewing contracts, iterating on technical documentation, or editing official correspondence—comparing different versions of a Word document is a frequent and critical task. While Microsoft Word provides a built-in "Compare" feature, manual processing falls short when you need to handle batch operations or integrate document diffing into automated CI/CD and business pipelines.

This article demonstrates how to automate Word document comparison using Python and the Free Spire.Doc for Python library.

Note: Free Spire.Doc for Python is a free-to-use library (with certain page limitations). It runs independently of Microsoft Office and works seamlessly in any standard Python environment.


1. Environment Setup

1.1 Installing the Library

Install the free Python library directly via pip:

pip install Spire.Doc.Free
Enter fullscreen mode Exit fullscreen mode

1.2 Core API Overview

The Document class provides the Compare() method, which compares the current document against another. Its signature is:

document.Compare(Document document, string author)
Enter fullscreen mode Exit fullscreen mode
  • document: The other Document object to compare against.
  • author: The reviewer name that will appear in the revision markers.

After comparison, all differences are recorded as Track Changes in the original document object. Saving it produces a standard Word file with visible revision marks.


2. Basic Document Comparison

2.1 Minimal Code Example

The following minimal script loads two documents, compares them, and outputs a result file with all revisions:

from spire.doc import *
from spire.doc.common import *

# Load the original document (baseline version)
firstDoc = Document("Original.docx")

# Load the revised document (target version)
secondDoc = Document("Revised.docx")

# Compare: identify all changes in secondDoc relative to firstDoc
# The second argument specifies the reviewer name shown in the revision marks
firstDoc.Compare(secondDoc, "System Auditor")

# Save the result as a new document
firstDoc.SaveToFile("Comparison_Result.docx", FileFormat.Docx2016)

# Release resources
firstDoc.Dispose()
secondDoc.Dispose()
Enter fullscreen mode Exit fullscreen mode

The logic is concise: instantiate two Document objects, call Compare(), and save. The library handles all the heavy lifting.

2.2 Output Result

The generated Comparison_Result.docx contains full revision marks:

  • Additions are shown with underlines or in a distinct color.
  • Deletions are shown with strikethrough.
  • Each change includes the reviewer's name and timestamp metadata.

3. Customizing Comparison Behavior

In many real‑world cases, you may want to ignore stylistic changes (e.g., font, size, color) and focus solely on content modifications. The CompareOptions class gives you fine‑grained control over what differences are flagged.

3.1 Ignoring Formatting Differences

from spire.doc import *
from spire.doc.common import *

firstDoc = Document("Original.docx")
secondDoc = Document("Revised.docx")

# Configure comparison options
compareOptions = CompareOptions()
compareOptions.IgnoreFormatting = True

# Pass the options into the comparison
firstDoc.Compare(secondDoc, "System Auditor", compareOptions)

firstDoc.SaveToFile("Comparison_Result_ContentOnly.docx", FileFormat.Docx2016)

firstDoc.Dispose()
secondDoc.Dispose()
Enter fullscreen mode Exit fullscreen mode

With IgnoreFormatting = True, only text insertions, deletions, and rewording are marked; all formatting changes are silently ignored.

3.2 Additional Options

Depending on your library version, CompareOptions may offer further controls:

Property Description
IgnoreFormatting Ignore font, size, color, and paragraph styling.
IgnoreHeadersAndFooters Ignore differences in headers and footers.
TextCompareLevel Controls comparison granularity; defaults to word-level (Word).

4. Summary

With just a few lines of Python, the Free Spire.Doc library turns Word document comparison into a fast, reliable, and automated process. Key benefits include:

  • Automation-ready: Easily scale to batch processing hundreds of documents.
  • Environment‑independent: No Microsoft Office installation required.
  • Highly customizable: Fine‑tune which differences to flag (formatting, headers, granularity).
  • Standard output: Generates native Word documents with familiar Track Changes markup.

Whether you're streamlining document auditing, version control, or regulatory compliance workflows, this approach eliminates tedious manual checks—saving time, reducing human error, and empowering teams to focus on higher‑value decisions.

Top comments (0)