sinhsac.github.io / document-to-markdown

Document to Markdown

Local, zero-config Python CLI & library converting PDFs, scanned images, and Excel workbooks into clean, AI/RAG-ready Markdown using PyMuPDF, PaddleOCR / PP-StructureV3, and Tesseract fallback.

Python 3.10+ PaddleOCR · PP-StructureV3 PDF · Images · Excel 100% Local & Private MIT License

Supported Formats & Capabilities

Input Extensions What It Handles
PDF .pdf Native text extraction (PyMuPDF) + automatic OCR on scanned or image-heavy pages (≥35% image area), multi-column reading order, repeated header/footer removal, password prompt (--password), optional image export (--assets).
Images .png, .jpg, .jpeg, .webp, .bmp, .tif, .tiff Automatic EXIF orientation fix, multi-page TIFF support, layout/table reconstruction via PP-Structure or line-grouped plain OCR, automatic Tesseract fallback.
Excel .xlsx, .xlsm Multi-region sheet splitting into separate Markdown tables, merged-cell expansion, 3-tier formula resolution (cached value → headless LibreOffice → pure-Python formulas), numeric alignment.
Clean Output by Default

Only the single .md file is written by default — no clutter folders. Pass --assets to export extracted images into <stem>_assets/ and --meta to emit a <stem>.meta.json sidecar.

Installation

Requires Python 3.10+. Install directly from GitHub in one command:

# Base install (native PDF + Excel)
pip install git+https://github.com/sinhsac/document-to-markdown.git

# Recommended: add PaddleOCR + PP-Structure layout/table analysis
pip install "document-to-markdown[ocr] @ git+https://github.com/sinhsac/document-to-markdown.git"

# Optional extras: Tesseract fallback or pure-Python Excel formula engine
pip install "document-to-markdown[tesseract] @ git+https://github.com/sinhsac/document-to-markdown.git"
pip install "document-to-markdown[excel-calc] @ git+https://github.com/sinhsac/document-to-markdown.git"

# Install all extras
pip install "document-to-markdown[all] @ git+https://github.com/sinhsac/document-to-markdown.git"

From Local Source (Editable Install)

git clone https://github.com/sinhsac/document-to-markdown.git
cd document-to-markdown
pip install -e ".[ocr]"

Standalone executables (Linux, Windows .exe, macOS — bundling native PDF, Excel, and Tesseract OCR) are also published on the Releases page. On the first OCR run, PaddleOCR downloads its detection/recognition/layout models once locally.

CLI Quick Start

Run via document-to-markdown or python -m document_converter.cli:

# Convert a PDF (outputs report.md only)
document-to-markdown report.pdf

# Convert a phone photo / scan (auto EXIF rotation + PP-Structure / PaddleOCR)
document-to-markdown scan.png

# Bilingual English + Vietnamese PDF formatted cleanly for AI/RAG ingestion
document-to-markdown report.pdf --mode ai --lang eng+vie

# Export extracted PDF images (_assets/) and metadata sidecar (.meta.json)
document-to-markdown report.pdf --assets --meta

# Batch convert an entire folder recursively
document-to-markdown data/input -o data/output --mode ai

# Convert an Excel workbook
document-to-markdown report.xlsx -o report.md

# Faster CPU OCR using lightweight mobile PP-OCRv5 models
document-to-markdown scan.pdf --ocr-profile mobile --ocr-version PP-OCRv5

# Disable PP-Structure layout analysis (faster plain OCR only)
document-to-markdown complex.pdf --no-structure

CLI Options Reference

Option Default / Values Description
inputspath(s)One or more files or directories to convert recursively.
-o, --output<input>.mdOutput Markdown file path or destination directory.
--modebalanced | fidelity | aiOutput style (ai includes source comments and suppresses inline warning blockquotes).
--ocrauto | paddle | tesseract | offOCR engine (auto prefers PaddleOCR/PP-Structure and falls back to Tesseract).
--langvie (default)OCR language (vie covers Vietnamese + English/Latin in PaddleOCR; supports eng+vie, etc.).
--ocr-profileauto | mobile | serverPaddleOCR model profile (mobile runs significantly faster on CPU).
--ocr-versionunsetModel version such as PP-OCRv5, PP-OCRv4, PP-OCRv3.
--det-model / --rec-modelunsetExplicit PaddleOCR detection/recognition model names (e.g. PP-OCRv5_mobile_det).
--no-structureflagSkip PP-Structure layout/table parsing and use plain OCR.
--assets, --imagesflagExport PDF images into <stem>_assets/.
--metaflagWrite machine-readable <stem>.meta.json sidecar.
--previewflagConvert only the first 3 pages of a PDF.
--passwordflagPrompt for password on encrypted PDFs.
--no-recalculateflagSkip LibreOffice / pure-Python Excel formula recalculation.

Python API

from document_converter import convert_file

output_md = convert_file(
    "report.pdf",
    "output/report.md",
    mode="balanced",       # "balanced" | "fidelity" | "ai"
    ocr="auto",            # "auto" | "paddle" | "tesseract" | "off"
    ocr_lang="vie",        # "vie", "eng", "eng+vie", ...
    ocr_profile="auto",    # "auto" | "mobile" | "server"
    ocr_version=None,      # e.g. "PP-OCRv5", "PP-OCRv4"
    det_model=None,        # e.g. "PP-OCRv5_mobile_det"
    rec_model=None,        # e.g. "latin_PP-OCRv5_mobile_rec"
    export_images=False,   # True to create output/report_assets/
    export_meta=False,     # True to create output/report.meta.json
)

How Processing Works & Known Limitations

  • Hybrid PDF Routing: Native text pages are extracted directly via PyMuPDF with multi-column clustering; pages with little text or ≥35% image coverage automatically route through OCR.
  • 3-Tier Excel Formula Evaluation: Resolves cached .xlsx values first, then LibreOffice headless recalculation (soffice on PATH), then pure-Python formulas, falling back to formula text with a warning if unresolved.
  • Limitations: Convert legacy .xls to .xlsx first; for pages mixing two unrelated writing systems (e.g. Vietnamese + Japanese on the same page), pass --ocr tesseract with system language packs.