fromgroupdocs.parserimportParser# Open the file streamwithopen("sample.pdf","rb")asstream:# Create an instance of Parser class with the streamwithParser(stream)asparser:# Get the document infodoc_info=parser.get_document_info()# Print document informationprint(f"File type: {doc_info.file_type.file_format}")print(f"Page count: {doc_info.page_count}")print(f"File size: {doc_info.size} bytes")
The following sample file is used in this example: sample.pdf
It’s useful to check document properties before performing extraction operations:
fromgroupdocs.parserimportParserdefprocess_document(file_path):withParser(file_path)asparser:# Get document informationdoc_info=parser.get_document_info()print(f"Processing: {file_path}")print(f"Type: {doc_info.file_type.file_format}")print(f"Pages: {doc_info.page_count}")print(f"Size: {doc_info.size/1024:.2f} KB")# Process based on page countifdoc_info.page_count>0:print("Document has pages, proceeding with text extraction...")text_reader=parser.get_text()iftext_readerisnotNone:print(text_reader.read_to_end())else:print("Document has no pages or page count is not available")# Process different document typesprocess_document("sample.pdf")
The following sample file is used in this example: sample.pdf
Processing: sample.pdf
Type: Portable Document Format File
Pages: 4
Size: 608.18 KB
Document has pages, proceeding with text extraction...
Investor relations
Annual Report 2025 | Port Ellery
Annual Report 2025
[TRUNCATED]
For multi-page documents, you can also get information about individual pages:
fromgroupdocs.parserimportParserwithParser("./sample.docx")asparser:# Get document infodoc_info=parser.get_document_info()print(f"Total pages: {doc_info.page_count}")# Iterate through pagesforpage_indexinrange(doc_info.page_count):# Extract text from each pageprint(f"\n--- Page {page_index+1} ---")text_reader=parser.get_text(page_index)iftext_readerisnotNone:page_text=text_reader.read_to_end()print(f"Characters: {len(page_text)}")
The following sample file is used in this example: sample.docx
If a document format doesn’t support certain features, the API returns appropriate values:
fromgroupdocs.parserimportParserfromgroupdocs.parser.exceptionsimportUnsupportedDocumentFormatException# A Word document, and an audio recording that isn't a documentforpathin("./sample.docx","./voice-memo.wav"):try:withParser(path)asparser:doc_info=parser.get_document_info()print(f"{path}: {doc_info.file_type.file_format}, {doc_info.page_count} pages")# Some formats may not have page countifdoc_info.page_count==0:print("Page count is not available for this format")exceptUnsupportedDocumentFormatException:print(f"{path}: this document format isn't supported")exceptFileNotFoundError:print(f"File not found: {path}")
The following sample file is used in this example: sample.docx
The following sample file is used in this example: voice-memo.wav
./sample.docx: Microsoft Word Open XML Document, 2 pages
./voice-memo.wav: this document format isn't supported
Along with the full-featured library, we provide a free online document parser app. You are welcome to extract data from PDF, DOCX, XLSX, and more with our Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.