Call the get_supported_file_types method of the FileType class
Enumerate through the collection of FileType objects
The following code snippet shows how to obtain a list of supported file formats:
Get all supported file formats
fromgroupdocs.parser.optionsimportFileType# Get all supported file typessupported_file_types=FileType.get_supported_file_types()# Iterate through the collectionforfile_typeinsorted(supported_file_types,key=lambdax:x.extension):print(f"{file_type.extension} - {file_type.file_format}")print(f"\nTotal supported formats: {len(supported_file_types)}")
Filter supported formats by category
You can filter supported formats based on your needs:
fromgroupdocs.parser.optionsimportFileType# Get all supported file typessupported_file_types=FileType.get_supported_file_types()# Define format categoriesword_formats=[".doc",".docx",".docm",".dot",".dotx",".dotm",".odt",".ott",".rtf"]excel_formats=[".xls",".xlsx",".xlsm",".xlsb",".xlt",".xltx",".xltm",".ods"]pdf_formats=[".pdf"]print("Word Processing Formats:")forfile_typeinsupported_file_types:iffile_type.extension.lower()inword_formats:print(f" {file_type.extension} - {file_type.file_format}")print("Spreadsheet Formats:")forfile_typeinsupported_file_types:iffile_type.extension.lower()inexcel_formats:print(f" {file_type.extension} - {file_type.file_format}")print("PDF Formats:")forfile_typeinsupported_file_types:iffile_type.extension.lower()inpdf_formats:print(f" {file_type.extension} - {file_type.file_format}")
Check if a specific format is supported
You can check if a particular file format is supported:
fromgroupdocs.parser.optionsimportFileTypedefis_format_supported(extension):"""Check if a file format is supported by GroupDocs.Parser"""supported_file_types=FileType.get_supported_file_types()forfile_typeinsupported_file_types:iffile_type.extension.lower()==extension.lower():returnTruereturnFalse# Check various formatsformats_to_check=[".pdf",".docx",".xlsx",".txt",".unknown"]forextinformats_to_check:ifis_format_supported(ext):print(f"{ext} - Supported")else:print(f"{ext} - Not Supported")
You can retrieve detailed information about each supported format:
fromgroupdocs.parser.optionsimportFileType# Get all supported file typessupported_file_types=FileType.get_supported_file_types()# Display format detailsprint("Supported Document Formats:\n")print(f"{'Extension':<12}{'Format Name':<50}")print("-"*62)forfile_typeinsorted(supported_file_types,key=lambdax:x.extension):print(f"{file_type.extension:<12}{file_type.file_format:<50}")
Practical usage example
Here’s a practical example of validating user input:
importosfromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportFileTypedefprocess_file(file_path):"""Process a file if its format is supported"""# Get file extension_,ext=os.path.splitext(file_path)# Check if format is supportedsupported_file_types=FileType.get_supported_file_types()is_supported=any(ft.extension.lower()==ext.lower()forftinsupported_file_types)ifnotis_supported:print(f"Error: File format '{ext}' is not supported")returnFalse# Process the filewithParser(file_path)asparser:doc_info=parser.get_document_info()print(f"Processing: {file_path}")print(f"Format: {doc_info.file_type.file_format}")print(f"Pages: {doc_info.page_count}")# Extract texttext_reader=parser.get_text()iftext_readerisNone:print("Text extraction isn't supported for this format")returnFalsetext=text_reader.read_to_end()print(f"Text extraction successful: {len(text)} characters")returnTrue# Example usageprocess_file("sample.pdf")process_file("sample.docx")
The following sample file is used in this example: sample.pdf
The following sample file is used in this example: sample.docx
Processing: sample.pdf
Format: Portable Document Format File
Pages: 4
Text extraction successful: 4414 characters
Processing: sample.docx
Format: Microsoft Word Open XML Document
Pages: 2
Text extraction successful: 2584 characters
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.