Working with Text

Beyond basic text extraction, you can search inside documents and control how text is returned. This page shows common tasks; consult the API reference for the full list of text-related methods and options.

Search for text

from groupdocs.parser import Parser
from groupdocs.parser.options import SearchOptions

with Parser("sample.pdf") as parser:
    # match_case, match_whole_word, use_regular_expression, search_by_pages
    options = SearchOptions(False, False, False, True)
    results = parser.search("harvest", options)
    if results is None:
        print("Search isn't supported for this format.")
    else:
        for result in results:
            print(f"Found on page {result.page_index + 1}: {result.text}")

The following sample file is used in this example: sample.pdf

Notes

  • parser.search() returns occurrences with matched text; set search_by_pages in SearchOptions to get page indexes.
  • For regex or case-sensitive searches, use search options from groupdocs.parser.options (see the API reference for available parameters).
  • Combine search with get_text() or get_text() per page to extract surrounding content.

Extract formatted or structured text

GroupDocs.Parser can return formatted text (HTML/Markdown/plain) and structured text (headings, lists, tables) when supported by the format.

from groupdocs.parser import Parser
from groupdocs.parser.options import FormattedTextOptions, FormattedTextMode

with Parser("sample.pptx") as parser:
    # Extract formatted text as Markdown (HTML and PLAIN_TEXT modes are also available)
    reader = parser.get_formatted_text(FormattedTextOptions(FormattedTextMode.MARKDOWN))
    print(reader.read_to_end() if reader is not None else "Formatted text isn't available.")

The following sample file is used in this example: sample.pptx

See the API reference for the formatted and structured text methods supported in Python via .NET.

Close
Loading

Analyzing your prompt, please hold on...

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