Comparison of Word documents is performed in various cases to identify the differences. Various online tools allow you to compare Word documents, however, you may need to implement the comparison feature from within your applications. To achieve it, this article shows how to compare two Word documents in Python.
- Python Library for Comparing Word Documents
- Compare Two Word Documents
- Additional Options for Word Document Comparison
Python Library to Compare Word Documents
To compare DOCX or DOC documents, we will use Aspose.Words for Python. The said library allows you to automate word processing features from within your Python applications. You can install the library from PyPI using the following pip command.
pip install aspose-words
Compare Two Word Documents in Python
Aspose.Words for Python makes it quite easy for you to compare two Word documents. In the comparison process, the changes are posted in the first document as revisions. The following are the steps to compare two Word documents in Python.
- First, load the first Word document using Document class.
- Then, load the second document using Document class.
- Call compare(Document, string, datetime.today()) method using the Document object of first document.
- Check if documents are different using Document.revisions.count property.
- Finally, save the first document to get the revisions.
The following code sample shows how to compare two Word documents.
The following is the screenshot of the comparison result.
Additional Options for Comparing Word Documents
Aspose.Words also provides some additional options to control the document comparison process. For example, you can ignore specific types of elements such as headers, footers, comments, etc. Also, you can set granularity to specify whether to compare the documents by characters or words. The following are the steps to set these options.
- First, load the first Word document using Document class.
- Then, load the second document using Document class.
- Create an instance of CompareOptions class and set desired options.
- Call compare(Document, string, datetime.today(), CompareOptions) method using the Document object of first document.
- Check if documents are different using Document.revisions.count property.
- Finally, save the first document to get the revisions.
The following code sample shows how to use additional options while comparing two Word documents.
Get a Free API License
You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.
Conclusion
In this article, you have learned how to compare two Word documents in Python. Moreover, you have seen how to control the document comparison operation using different options. Besides, you can explore other features of Aspose.Words for Python using the documentation. In case of any questions, you can ask us via our forum.