Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Mar 27, 2025 1:01 pm

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)

Sanket98
 
Posts: 3
Joined: Wed Jan 15, 2025 1:08 pm

Fri Mar 28, 2025 3:22 am

Hello,

Thansk for your inquiry.
To help us locate your problem faster, please provide us with your test file. You can upload it to the attachment here or send it to this email: [email protected]. Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Fri Mar 28, 2025 7:57 am

Attaching 2 sample word documents. We are comparing two documents, both containing tracked changes and revisions. However, we do not want to use AcceptAllChanges() because the user needs to review and either accept or reject the changes manually. Therefore, we need a way to compare the documents without altering their tracked changes.

Sanket98
 
Posts: 3
Joined: Wed Jan 15, 2025 1:08 pm

Fri Mar 28, 2025 8:52 am

Hello,

Thanks for your reply.
Please note that our product follows Microsoft's specifications, which do not support comparing documents containing tracked changes/revisions - even Microsoft Word itself throws an error in such cases (as shown in the attached screenshot). Therefore, we're unable to fulfill your comparison request for revised documents. Instead, we recommend using the provided code snippet for your comparison needs. Please let us know if you require any further assistance.
Code: Select all
from spire.doc import *
firstDoc = Document("Test_1.docx") 
if(firstDoc.HasChanges):
    firstDoc.AcceptChanges()
secondDoc = Document("Test_1.docx") 
if(secondDoc.HasChanges):
    secondDoc.AcceptChanges()
firstDoc.Compare(secondDoc, "e-iceblue") 
firstDoc.SaveToFile("res.docx", FileFormat.Docx2016) 
firstDoc.Dispose()
secondDoc.Dispose()

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc