This guide demonstrates the essential steps to get started with GroupDocs.Parser for Python via .NET and perform basic document parsing operations.
Prerequisites
Before you begin, ensure you have:
Python 3.5 or higher installed
GroupDocs.Parser for Python via .NET installed (see Installation)
Extract Text from a Document
The most common task is extracting text from documents. Here’s how to do it:
fromgroupdocs.parserimportParser# Create an instance of Parser classwithParser("./sample.pdf")asparser:# Extract text from the documenttext_reader=parser.get_text()iftext_readerisNone:print("Text extraction isn't supported for this format")else:# Print the extracted textprint(text_reader.read_to_end())
The following sample file is used in this example: sample.pdf
Get Document Information
You can retrieve basic information about a document:
fromgroupdocs.parserimportParser# Create an instance of Parser classwithParser("./sample.pdf")asparser:# Get document infoinfo=parser.get_document_info()print(f"File type: {info.file_type.file_format}")print(f"Page count: {info.page_count}")print(f"Size: {info.size} bytes")
The following sample file is used in this example: sample.pdf
Extract Metadata
Extract metadata properties from documents:
fromgroupdocs.parserimportParser# Create an instance of Parser classwithParser("./sample.pdf")asparser:# Extract metadatametadata=parser.get_metadata()ifmetadataisNone:print("Metadata extraction isn't supported for this format")else:foriteminmetadata:print(f"{item.name}: {item.value}")
The following sample file is used in this example: sample.pdf
Extract Images
Extract images from documents:
fromgroupdocs.parserimportParser# Create an instance of Parser classwithParser("./sample.pdf")asparser:# Extract imagesimages=parser.get_images()ifimagesisNone:print("Image extraction isn't supported for this format")else:print(f"Found {len(images)} images")fori,imageinenumerate(images,start=1):# Save the image to a file (the extension includes the dot, e.g. ".jpeg")image.save(f"image_{i}{image.file_type.extension}")
The following sample file is used in this example: sample.pdf
Extract Text from Specific Page
Extract text from a particular page:
fromgroupdocs.parserimportParser# Create an instance of Parser classwithParser("./sample.pdf")asparser:# Extract text from the first page (page index is 0-based)text_reader=parser.get_text(0)iftext_readerisNone:print("Page text extraction isn't supported for this format")else:print(text_reader.read_to_end())
The following sample file is used in this example: sample.pdf
Check Format Support
Before processing a document, you can check if the format is supported:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportFileType# Detect the file typefile_info=Parser.get_file_info("./sample.pdf")iffile_info.file_type==FileType.UNKNOWN:print("Format is not supported")else:print(f"Format is supported: {file_info.file_type.file_format}")# Process the documentwithParser("./sample.pdf")asparser:text_reader=parser.get_text()iftext_readerisnotNone:print(text_reader.read_to_end())
The following sample file is used in this example: sample.pdf
Next Steps
Now that you’ve learned the basics, explore more advanced features: