Hello everyone,
I’m currently working with the Spire.Doc Python library to compare two Word documents. One of the documents (our “original”) contains tracked changes (redlines) from previous edits. In our workflow, we want to keep the second document’s revisions intact (as they’ve been manually accepted or rejected by a user), but we need to clean the original document of its revisions before performing the comparison.
However, even after calling AcceptAllChanges() on the original document and saving it as a cleaned copy, I still encounter the error:
Here’s a simplified version of the code I’m using:
from spire.doc import *
def tracker(chart_id):
# Load the original document
file_path = "gvtmp/" + chart_id + ".docx"
first = Document()
first.LoadFromFile(file_path)
# Accept all tracked changes in the original document
first.AcceptAllChanges()
# Save the cleaned original document
cleaned_file_path = "gvtmp/" + chart_id + "_cleaned.docx"
first.SaveToFile(cleaned_file_path, FileFormat.Docx2016)
print(f"Cleaned original document saved as {cleaned_file_path}")
# Reload the cleaned original document
firstDoc = Document()
firstDoc.LoadFromFile(cleaned_file_path)
# Load the second document (with its revisions intact)
secondDoc = Document()
secondDoc.LoadFromFile("gvtmp/Track_changed_documents.docx")
# Attempt to compare the documents
firstDoc.Compare(secondDoc, "Reviewer")
# Save the comparison result
output_file = "gvtmp/" + chart_id + "_new.docx"
firstDoc.SaveToFile(output_file, FileFormat.Docx2016)
print(f"Comparison completed. The result is saved as '{output_file}'.")
firstDoc.Dispose()
secondDoc.Dispose()
# Example usage:
tracker("your_chart_id")
However, when I try to compare the two documents after cleaning only the original, I receive the following error:
File "C:\Users\AppData\Roaming\Python312\site-packages\spire\doc\Document.py", line 642, in __call__
return self.f(self.instance, *args, **kwargs)
File "C:\Users\AppData\Roaming\Python312\site-packages\spire\doc\function.py", line 592, in run_type
return _convert(method(*args, **kwargs), return_type)
File "C:\Users\AppData\Roaming\Python312\site-packages\spire\doc\Document.py", line 1384, in Compare
CallFunctionGetDllObject(Document_Compare, self._ptr, pDocInPtr, authorPtr)
File "C:\Users\AppData\Roaming\Python312\site-packages\spire\doc\common\__init__.py", line 129, in CallFunctionGetDllObject
raise Spire.exception.SpireException(info)
Spire.exception.SpireException: Compared documents must not have revisions.: at sprghi.sprgh.a() 0x4ef
at #=zAN01.NtNDocument.Compare(IntPtr, IntPtr, IntPtr)