All conversion methods raise exceptions on failure. Use standard try/except with specific exception types:
fromgroupdocs.markdownimportMarkdownConverterfromgroupdocs.markdownimportDocumentProtectedException,InvalidFormatException,GroupDocsMarkdownExceptiondeferror_handling_example():"""Demonstrate error handling with specific exception types during conversion."""try:# Step 1: Attempt to convert the documentMarkdownConverter.to_file("annual-report.docx","error-handling.md")exceptDocumentProtectedException:# Step 2a: Handle password-protected documentsprint("Wrong or missing password.")exceptInvalidFormatException:# Step 2b: Handle corrupt or unsupported file formatsprint("File is corrupt or unsupported.")exceptGroupDocsMarkdownExceptionasex:# Step 2c: Handle any other conversion errorsprint(f"Conversion failed: {ex}")if__name__=="__main__":error_handling_example()
annual-report.docx is sample file used in this example. Click here to download it.
Document is password-protected and no password or wrong password was provided
InvalidFormatException
File is corrupt or has an unrecognized format
GroupDocsMarkdownException
General conversion error
Conversion warnings
Non-fatal issues are reported via ConvertResult.warnings:
fromgroupdocs.markdownimportMarkdownConverter,ConvertOptionsdefwarnings_example():"""Show how to inspect non-fatal conversion warnings after converting."""# Step 1: Open the spreadsheet with a context managerwithMarkdownConverter("cost-analysis.xlsx")asconverter:# Step 2: Configure row truncationoptions=ConvertOptions()options.max_rows=10# limit to first 10 data rows per sheet# Step 3: Convert using keyword argument for optionsresult=converter.convert("warnings.md",convert_options=options)# Step 4: Inspect non-fatal warnings (e.g., truncation notices)forwarninginresult.warnings:print(f"Warning: {warning}")if__name__=="__main__":warnings_example()
cost-analysis.xlsx is sample file used in this example. Click here to download it.