Get Source and Target Text from Files

GroupDocs.Comparison for Python via .NET records the source and target text for each detected change. Read source_text and target_text on each ChangeInfo to surface the before/after pair.

Example 1: Get source and target text (file paths)

from groupdocs.comparison import Comparer

def get_source_and_target_text():
    with Comparer("./source.docx") as comparer:
        comparer.add("./target.docx")
        comparer.compare("./result.docx")
        for change in comparer.get_changes():
            print(f"Source text: {change.source_text}")
            print(f"Target text: {change.target_text}\n")

if __name__ == "__main__":
    get_source_and_target_text()

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

Use case: extract changed text pairs to power side-by-side diff review tools.

Example 2: Get source and target text (streams)

from groupdocs.comparison import Comparer

def get_source_and_target_text_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("./result.docx")
            for change in comparer.get_changes():
                print(f"Source text: {change.source_text}")
                print(f"Target text: {change.target_text}\n")

if __name__ == "__main__":
    get_source_and_target_text_stream()

source.docx is the source file used in this example. Click here to download it.

Binary file (DOCX, 25 KB)

Download full output

Use case: feed structured change text into analytics or translation workflows.

Close
Loading

Analyzing your prompt, please hold on...

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