This guide shows you how to convert documents to Markdown with GroupDocs.Markdown for Python via .NET. You’ll have working code in under 2 minutes.
How it works
flowchart LR
A["Input Document\n(DOCX, PDF, XLSX, EPUB, ...)"]
B["MarkdownConverter"]
C["Markdown Output\n(.md file or string)"]
A --> B --> C
Prerequisites
Install the package from PyPI:
pip install groupdocs-markdown-net
Import the module:
fromgroupdocs.markdownimportMarkdownConverter
Example 1: Convert Word to Markdown
The simplest conversion – one line of code:
fromgroupdocs.markdownimportMarkdownConverterdefconvert_word_to_markdown():"""Convert a Word document to Markdown in one line of code."""# Convert a DOCX file directly to a Markdown file on diskMarkdownConverter.to_file("business-plan.docx","quick-start-word.md")if__name__=="__main__":convert_word_to_markdown()
business-plan.docx is sample file used in this example. Click here to download it.
Save a PDF to Markdown with images extracted to a folder:
fromgroupdocs.markdownimportMarkdownConverter,ConvertOptions,ExportImagesToFileSystemStrategydefconvert_pdf_with_images():"""Convert a PDF to Markdown with images exported to a folder on disk."""# Step 1: Configure image export with relative pathsstrategy=ExportImagesToFileSystemStrategy("output/images")strategy.images_relative_path="images"# Step 2: Assign the strategy to conversion optionsoptions=ConvertOptions()options.image_export_strategy=strategy# Step 3: Convert and save using keyword argument for optionsMarkdownConverter.to_file("business-plan.pdf","output/report.md",convert_options=options)# Images saved to output/images/# Markdown references: if__name__=="__main__":importosos.makedirs("output/images",exist_ok=True)convert_pdf_with_images()
business-plan.pdf is sample file used in this example. Click here to download it.
Convert a spreadsheet with column truncation and front matter:
fromgroupdocs.markdownimportMarkdownConverter,ConvertOptions,MarkdownFlavordefconvert_excel_with_options():"""Convert an Excel spreadsheet with column/row limits, front matter, and GFM flavor."""# Step 1: Configure spreadsheet-specific conversion optionsoptions=ConvertOptions()options.max_columns=8# limit to first 8 columnsoptions.max_rows=50# limit to first 50 data rowsoptions.include_front_matter=True# add YAML metadata blockoptions.flavor=MarkdownFlavor.GIT_HUB# GitHub Flavored Markdown# Step 2: Open the spreadsheet with a context managerwithMarkdownConverter("cost-analysis.xlsx")asconverter:# Step 3: Convert and save the Markdown outputconverter.convert("quick-start-excel.md",convert_options=options)if__name__=="__main__":convert_excel_with_options()
cost-analysis.xlsx is sample file used in this example. Click here to download it.