Get Source and Target Text from Files

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Comparison MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-comparison -- dnx GroupDocs.Comparison.Mcp --yes
Folders, licensing and more →

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.