Compare Documents

The change-detection algorithm of GroupDocs.Comparison detects changes across many document parts and blocks:

  • Text blocks — paragraphs, words, and characters.
  • Tables.
  • Images.
  • Shapes.

The result document highlights detected changes with colour:

  • Added — blue.
  • Modified — green.
  • Style — green.
  • Deleted — red.

The styling scheme is fully customizable — see Customize changes styles.

To compare two documents, follow these steps:

  1. Instantiate a Comparer with the source document path or stream.
  2. Call add() and specify the target document path or stream.
  3. Call compare() and specify the result file path or output stream.

Example 1: Compare documents from file paths

The simplest way to compare two documents is by passing file paths to Comparer.

from groupdocs.comparison import Comparer

def compare_documents():
    # Initialize Comparer with the source file path
    with Comparer("./source.docx") as comparer:
        # Add the target file and run the comparison
        comparer.add("./target.docx")
        comparer.compare("./result.docx")

if __name__ == "__main__":
    compare_documents()

source.docx is the source file used in this example. Click here to download it.

target.docx is the target file used in this example. Click here to download it.

Binary file (DOCX, 25 KB)

Download full output

The output file looks like this:

Example 2: Compare documents from streams

You can also feed Comparer open file streams — useful when the source and target arrive over the network, from a database, or from another in-memory source.

from groupdocs.comparison import Comparer

def compare_documents_from_stream():
    # Open the source and target as binary streams
    with open("./source.docx", "rb") as source_stream, \
         open("./target.docx", "rb") as target_stream:
        with Comparer(source_stream) as comparer:
            comparer.add(target_stream)
            comparer.compare("./result.docx")

if __name__ == "__main__":
    compare_documents_from_stream()

source.docx is the source file used in this example. Click here to download it.

target.docx is the target file used in this example. Click here to download it.

Binary file (DOCX, 25 KB)

Download full output

Example 3: Compare documents and write the result to a stream

Combine input streams with an output stream to keep everything in memory:

from groupdocs.comparison import Comparer

def compare_documents_to_stream():
    with Comparer("./source.docx") as comparer:
        comparer.add("./target.docx")
        with open("./result.docx", "wb") as out_stream:
            comparer.compare(out_stream)

if __name__ == "__main__":
    compare_documents_to_stream()

source.docx is the source file used in this example. Click here to download it.

target.docx is the target file used in this example. Click here to download it.

Binary file (DOCX, 25 KB)

Download full output

Next steps

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.