Load text from string

On this page

GroupDocs.Comparison allows you to compare values from the String type variables.

To compare text from variables, follow these steps:

  1. Instantiate the LoadOptions object. Set the LoadText property to true (this indicates that the passed string contains a text to be compared, not a file path).
  2. Instantiate the Comparer object. Specify the source variable of the String type and the LoadOptions object created in the previous step.
  3. Call the add() method. Specify the target variable of the String type and the LoadOptions object created in the previous step.
  4. Call the Comparer method.
  5. Call the getResultString method to get a string with the comparison result.

The following code snippet shows how to load values from variables:

import groupdocs.comparison as gc

def load_text_from_string(output_file_path):
    # Initialize LoadOptions and set to load text
    load_options = gc.options.LoadOptions()
    load_options.load_text = True

    # Initialize comparer with the source text using LoadOptions
    with gc.Comparer("source text", load_options) as comparer:
        # Add the target text for comparison using LoadOptions
        comparer.add("target text", load_options)
        
        # Compare the texts and save the result
        comparer.compare(output_file_path)

        # Print the result string
        print("Result string: \n" + comparer.get_result_string())
    
    # Log the success message with the output file path
    print(f"\nDocuments compared successfully.\nCheck output in {output_file_path}.")

The result is as follows:

On this page