Retrieval-augmented generation (RAG) systems need documents in a clean, structured text format for chunking and embedding. Markdown is ideal – it preserves document structure (headings, lists, tables) while being easy to parse.
flowchart LR
A["PDF / DOCX / XLSX"]
B["GroupDocs.Markdown"]
C["Markdown"]
D["Text Chunking"]
E["Vector Embeddings"]
F["LLM Query"]
A --> B --> C --> D --> E --> F
Basic conversion for RAG
importrefromgroupdocs.markdownimportMarkdownConverter,ConvertOptions,SkipImagesStrategy,MarkdownFlavordefconvert_for_rag():"""Convert a PDF to Markdown for RAG pipelines, then split into chunks by heading."""# Step 1: Configure conversion for text-only RAG (skip images)options=ConvertOptions()options.image_export_strategy=SkipImagesStrategy()options.flavor=MarkdownFlavor.COMMON_MARK# Step 2: Convert the document and save it to a Markdown fileMarkdownConverter.to_file("business-plan.pdf","convert-for-rag-basic.md",convert_options=options)# Step 3: Read the result back and split it into chunks by heading markerswithopen("convert-for-rag-basic.md","r",encoding="utf-8")asf:markdown=f.read()chunks=[cforcinre.split(r"\n#{1,2} ",markdown)ifc.strip()]# Step 4: Write a chunk summary to a text file (ready to feed an embedding model)withopen("convert-for-rag-basic.txt","w",encoding="utf-8")asf:fori,chunkinenumerate(chunks,1):f.write(f"Chunk {i} ({len(chunk)} chars): {chunk[:80]}...\n")if__name__=="__main__":convert_for_rag()
business-plan.pdf is sample file used in this example. Click here to download it.
**Meridian Outdoor Co. — Business Plan**
FY2026 Strategic Plan
**Table of Contents**
Meridian Outdoor Co. — Business Plan .........................................................................................1FY2026 Strategic Plan.................................................................................................................1Table of Contents.............................................................................................................................2
1. Ex
[TRUNCATED]
importosimportglobfromgroupdocs.markdownimportMarkdownConverter,ConvertOptions,SkipImagesStrategy,GroupDocsMarkdownExceptiondefbatch_convert_for_rag():"""Batch-convert all PDFs in a folder to Markdown for RAG ingestion."""# Step 1: Configure conversion to skip images (text-only RAG)options=ConvertOptions()options.image_export_strategy=SkipImagesStrategy()# Step 2: Find all PDF files in the documents folderfiles=glob.glob("documents/*.pdf")# Step 3: Convert each file, handling errors gracefully, and write a logwithopen("batch-convert-for-rag.txt","w",encoding="utf-8")aslog:forfileinfiles:output_path=os.path.splitext(file)[0]+".md"try:MarkdownConverter.to_file(file,output_path,convert_options=options)log.write(f"Converted: {file}\n")exceptGroupDocsMarkdownExceptionasex:log.write(f"Skipped {file}: {ex}\n")if__name__=="__main__":batch_convert_for_rag()