In some cases, it’s required to specify the document format manually to guarantee correct output produced by GroupDocs.Parser. The following are the cases when the document format must be specified manually:
Here are the steps to specify the document format:
Instantiate the LoadOptions object and pass the document format in the constructor
Create Parser object with the file path and LoadOptions
Call extraction methods as usual
Load Markdown document
The following example shows how to specify the document format for Markdown document:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportLoadOptions,FileType# Create LoadOptions for Markdown formatload_options=LoadOptions(FileType.MD)# Open the file streamwithopen("sample.md","rb")asstream:# Create an instance of Parser class with LoadOptionswithParser(stream,load_options)asparser:# Extract text from the Markdown documenttext_reader=parser.get_text()iftext_readerisnotNone:# Print the extracted text# Markdown is detected; text without special symbols is printedprint(text_reader.read_to_end())else:print("Text extraction isn't supported")
The following sample file is used in this example: sample.md
Harbor Lane Provisions: wholesale ordering guide
Welcome to Harbor Lane Provisions, the specialty food wholesaler on the Quayside in Port Ellery. This guide explains how to open an account, place orders and receive deliveries.
Opening an account
1. Fill in the account form at [harborlane.example/account](https://harborlane.example/account).
2. Send us your resale certificate.
3. Lena Park confirms your account within two working days.
[TRUNCATED]
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportLoadOptions,FileType# Create LoadOptions for OTP (OpenDocument Presentation Template) formatload_options=LoadOptions(FileType.OTP)# Load OTP documentwithParser("template.otp",load_options)asparser:# Get document infodoc_info=parser.get_document_info()print(f"File type: {doc_info.file_type.file_format}")print(f"Pages: {doc_info.page_count}")# Extract texttext_reader=parser.get_text()iftext_readerisnotNone:print(text_reader.read_to_end())
The following sample file is used in this example: template.otp
File type: OpenDocument Presentation Template
Pages: 2
Harbor Lane Provisions presentation title
Subtitle, presenter and date
Slide title
First point in Arial 18Second pointThird pointHarbor Lane colours: harbor navy, sea teal, sand and coral
You can create a helper function to detect and load documents with appropriate format:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportLoadOptions,FileTypeimportosdefget_file_format(file_path):"""Detect file format based on extension"""_,ext=os.path.splitext(file_path)ext=ext.lower()format_map={'.md':FileType.MD,'.markdown':FileType.MD,'.mhtml':FileType.MHTML,'.mht':FileType.MHTML,'.otp':FileType.OTP,}returnformat_map.get(ext)defload_document_with_format(file_path):"""Load document with appropriate format specification"""file_format=get_file_format(file_path)iffile_format:print(f"Loading with explicit format: {file_format}")load_options=LoadOptions(file_format)parser=Parser(file_path,load_options)else:print("Loading with auto-detection")parser=Parser(file_path)returnparser# Usage exampleswithload_document_with_format("webpage.mhtml")asparser:text_reader=parser.get_text()iftext_readerisnotNone:print(text_reader.read_to_end())
The following sample file is used in this example: webpage.mhtml
Loading with explicit format: MIME HTML File(.mhtml)
Subject: Harbor Lane Provisions - Wholesale specialty food Date: Thu, 15 Jan 2026 09:00:00 -0000 MIME-Version: 1.0 Content-Type: multipart/related; type="text/html"; boundary="----=_HarborLane_Archive_2026" ------=_HarborLane_Archive_2026 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Location: https://harborlane.example/
HYPERLINK "https://harborlane.example/" Home HYPERLINK "https://harborlane.examp
[TRUNCATED]
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportLoadOptionsdefsafe_load_with_format(file_path,file_format):"""Safely load a document with specific format and error handling"""try:# Create LoadOptionsload_options=LoadOptions(file_format)# Create Parser instancewithParser(file_path,load_options)asparser:# Verify loading was successfuldoc_info=parser.get_document_info()print(f"Document loaded: {doc_info.file_type.file_format}")# Extract texttext_reader=parser.get_text()iftext_readerisnotNone:returntext_reader.read_to_end()else:print("Text extraction not supported")returnNoneexceptExceptionase:print(f"Error loading document with format {file_format}: {e}")# Try loading without format specificationtry:print("Attempting to load with auto-detection...")withParser(file_path)asparser:text_reader=parser.get_text()iftext_readerisnotNone:returntext_reader.read_to_end()exceptExceptionase2:print(f"Auto-detection also failed: {e2}")returnNone# Example usagefromgroupdocs.parser.optionsimportFileTypetext=safe_load_with_format("document.md",FileType.MD)
The following sample file is used in this example: document.md
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.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.