Instantiate a Comparer with the source document path or stream.
Call add() and specify the target document path or stream.
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.
fromgroupdocs.comparisonimportComparerdefcompare_documents():# Initialize Comparer with the source file pathwithComparer("./source.docx")ascomparer:# Add the target file and run the comparisoncomparer.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.
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.
fromgroupdocs.comparisonimportComparerdefcompare_documents_from_stream():# Open the source and target as binary streamswithopen("./source.docx","rb")assource_stream, \
open("./target.docx","rb")astarget_stream:withComparer(source_stream)ascomparer: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.