Guide · Python

PDF to Markdown in Python: libraries and code

Python has several ways to turn a PDF into Markdown, and they solve different problems. This guide shows a structured converter, a plain text extractor and a lightweight LLM-oriented option.

Updated November 23, 2026 · Markdown2PDF by Hyperdyn

Choose the right level of extraction

ApproachBest forTrade-off
PyMuPDFFast local extraction with optional Markdown output.Check the AGPL/commercial license for your distribution.
pdfplumberText and table inspection with straightforward Python code.You still need to build Markdown structure yourself.
MarkItDownLLM-ready text from many file types.Not a high-fidelity publishing converter.
Marker / DoclingLayout-aware Markdown with tables and reading order.Heavier install and model downloads.

PyMuPDF: structured Markdown in a few lines

Current PyMuPDF builds can export a document to Markdown directly. Install the package and write the result to a UTF-8 file:

pip install --upgrade pymupdf
import pymupdf

doc = pymupdf.open("input.pdf")
markdown = doc.to_markdown()

with open("output.md", "w", encoding="utf-8") as handle:
    handle.write(markdown)

The Markdown export is designed for LLM and analysis workflows. It may preserve tables and reading order better than plain text, but it is still worth reviewing. PyMuPDF is dual-licensed under AGPL or a commercial license; confirm the license before shipping it in a proprietary product.

pdfplumber: extract text and control the cleanup

If you only need text, pdfplumber gives you page-level access and table helpers. This example joins page text with blank lines; it does not invent headings that are not in the PDF:

import pdfplumber

with pdfplumber.open("input.pdf") as pdf:
    pages = [(page.extract_text() or "") for page in pdf.pages]

with open("output.md", "w", encoding="utf-8") as handle:
    handle.write("\n\n".join(pages))

The output is plain text in a Markdown file, not a true structural conversion. Use this when predictable text is enough, or add your own heading and table rules after studying the document template.

MarkItDown: a small API for mixed documents

MarkItDown is convenient when the same job also has to handle Word, PowerPoint or HTML. Install the PDF extra and call the local conversion method:

pip install 'markitdown[pdf]'
from markitdown import MarkItDown

result = MarkItDown(enable_plugins=False).convert_local("input.pdf")
with open("output.md", "w", encoding="utf-8") as handle:
    handle.write(result.markdown)

Review the security guidance in the MarkItDown guide before exposing it to untrusted files.

Scanned PDFs need OCR, not hope

If extract_text() or to_markdown() returns nothing useful, the page is probably an image. OCR is the next step. PyMuPDF can call Tesseract or RapidOCR, and the larger document parsers include their own OCR pipelines.

OCR introduces language, rotation and scan-quality variables. Run a sample, check numbers and names carefully, and keep the original PDF as the source of truth.

A small cleanup pass that pays for itself

  1. Normalize heading levels so the document has one logical outline.
  2. Wrap code and commands in fenced blocks with a language tag.
  3. Repair tables and remove repeated page headers or footers.
  4. Join hyphenated line breaks, but leave intentional line breaks alone.
  5. Preview the Markdown before publishing or feeding it to another tool.

Frequently asked questions

What is the easiest Python library for PDF to Markdown?

For a short script, PyMuPDF is hard to beat because it can export Markdown directly. For layout-aware conversion with more structure, Marker or Docling are stronger but heavier options.

Why does my PDF return empty text in Python?

The PDF may be a scan with no text layer, or it may use fonts without a usable character map. Try OCR or a document parser that includes OCR, and check the original page visually.

Does Markdown2PDF offer a Python API?

No. Markdown2PDF is a browser editor that converts Markdown to PDF and HTML. The Python libraries in this guide are separate open-source projects for the reverse direction.