Accept or Reject Detected Changes

GroupDocs.Comparison for Python via .NET lets you programmatically accept or reject individual changes before producing the final result. This enables reviewer-style workflows: get the change list, decide per change, then apply the accepted ones.

Steps to accept or reject changes

  1. Compare two documents using Comparer.compare().
  2. Enumerate the changes via comparer.get_changes().
  3. For each ChangeInfo, set comparison_action to ComparisonAction.ACCEPT or ComparisonAction.REJECT.
  4. Call comparer.apply_changes() with ApplyChangeOptions(changes=...) to produce the merged result.

Example 1: Accept or reject changes (file paths)

from groupdocs.comparison import Comparer
from groupdocs.comparison.options import ApplyChangeOptions
from groupdocs.comparison.result import ComparisonAction

def accept_or_reject_changes():
    with Comparer("./source.docx") as comparer:
        comparer.add("./target.docx")
        comparer.compare()
        changes = comparer.get_changes()

        # Reject the first change; accept everything else
        if changes:
            changes[0].comparison_action = ComparisonAction.REJECT

        comparer.apply_changes("./result.docx", ApplyChangeOptions(changes=changes))

if __name__ == "__main__":
    accept_or_reject_changes()

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 result is as follows:

Accepted changesRejected changes

Example 2: Accept or reject changes (streams)

Same operation, but everything runs through streams — useful in services that don’t persist intermediate files.

from groupdocs.comparison import Comparer
from groupdocs.comparison.options import ApplyChangeOptions
from groupdocs.comparison.result import ComparisonAction

def accept_or_reject_changes_stream():
    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()
            changes = comparer.get_changes()
            if changes:
                changes[0].comparison_action = ComparisonAction.REJECT
            with open("./result.docx", "wb") as out_stream:
                comparer.apply_changes(out_stream, ApplyChangeOptions(changes=changes))

if __name__ == "__main__":
    accept_or_reject_changes_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

Close
Loading

Analyzing your prompt, please hold on...

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