doc-to-markdown
Converts DOCX/PDF/PPTX to high-quality Markdown with automatic post-processing. Fixes pandoc grid tables, simple tables, image paths, CJK bold spacing, attribute noise, and code blocks; for PDFs also strips OCR garbage blocks, repeated headers/footers/watermarks, and absolute image paths from pymupdf4llm output. Benchmarked best-in-class (7.6/10) against Docling, MarkItDown, Pandoc raw, and Mammoth. Trigger on "convert document", "docx to markdown", "parse word", "doc to markdown", "解析word", "转换文档".
1How It Works & Mechanism
Implemented via a two-layer architecture: 'Pandoc extraction + value-add post-processing'. SKILL.md explicitly fixes this architecture, asserting that core value lies in the 8 DOCX post-processing steps rather than the raw conversion itself.
**Architecture**: Pandoc (best-in-class extraction) + 8 post-processing fixes (our value-add).
Tool selection routes via Quick/Heavy dual modes: Quick mode runs a single tool rapidly, while Heavy mode executes multiple tools in parallel and merges segments (tables/images/headings/lists/paragraphs) by picking the best candidate according to quality scoring.
| PDF | pymupdf4llm | pymupdf4llm + markitdown |
DOCX post-processing is backed category-by-category by tests: all 8 cleanup tasks (grid/simple tables, image paths, pandoc attributes, CJK bold spacing, indented code blocks, escaped brackets, double-bracket links) map directly to tests in SKILL.md such as TestPostprocessPipeline and TestCjkBoldSpacing (15 cases).
| Grid tables (`+:---+`) | Single-column → blockquote, multi-column → pipe table | `TestPostprocessPipeline` |
PDF post-processing (since 2026-08-30) performs three tasks: stripping Tesseract OCR garbage blocks, removing cross-page repeated headers/footers/watermark lines (identified when a normalized line appears on ≥60% of pages), and converting absolute image paths to relative ones.
Repeated header/footer/watermark lines (same normalized line on ≥60% of pages, incl. diagonal watermarks) | Detected via pymupdf cross-page scan, removed from markdown; bold-wrapped and merged-with-page-number variants also caught | `TestRepeatingLines`
The quality loop is supported by validate_output.py: it provides Pass/Warn/Fail thresholds based on retention rates of text, tables, and images, and can generate HTML reports.
| Text Retention | >95% | 85-95% | <85% |
The best-in-class (7.6/10) claim is backed by in-repo benchmark documents: scoring Docling/MarkItDown/Pandoc/Mammoth dimension-by-dimension, concluding in favor of a pandoc foundation combined with a post-processing layer.
| 维度 | Docling (IBM) | MarkItDown (MS) | Pandoc | Mammoth | **doc-to-markdown(我们)** |
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | pandoc |
| cli | markitdown CLI(markitdown[pdf]) |
| package | pymupdf4llm / pymupdf(uv --with 拉取) |
| cli | pdftotext / pdftoppm(poppler,校验脚本用) |
4Recommended Governance & Evidence
- scripts/: Executes local scripts or commands: python scripts/convert.py (local parsing + post-processing)
- 外部端点: Exfiltrates local content or conversation data to third-party cloud storage
- 本地工作目录: File I/O and artifact output: -o writes markdown output; --assets-dir saves images to disk (flattening media/); extract_pdf_images/validate_out
5Second-Pass Verification
- [ok] 架构=Pandoc+8 后处理 — SKILL.md 原文与 convert.py 的 tool_map/pandoc 调用路径吻合;8 项清洗在 DOCX Post-Processing 表逐行列出。
- [ok] CJK 加粗间距规则 — 规则原文(含 CJK 即两侧补空格)在 SKILL.md 'CJK Bold Spacing — why and how' 节,测试类 TestCjkBoldSpacing 存在。
- [ok] PDF 水印剥离用 pymupdf 跨页扫描 — SKILL.md 声称 + convert.py _detect_repeating_lines 内 import pymupdf、doc=pymupdf.open(...) 属实。
- [ok] Heavy 模式并行多工具 — SKILL.md 'Run all applicable tools simultaneously' + 分 segment 评分合并准则表均在。
- [ok] 质量阈值 95/100% 等 — validate_output.py 存在 pdftotext 子进程调用,SKILL 阈值表为文档化契约。
- [ok] 7.6/10 best-in-class 声明 — references/benchmark-2026-03-22.md 含 5 工具维度评分表与 '综合最优' 结论;评分口径为内部基准、非独立第三方评测——已注明。
- [ok] 无网络外发/无凭证 — convert.py/validate_output.py/merge_outputs.py/extract_pdf_images.py 全量扫描:无 requests/urllib/http 调用点(仅测试 fixture 的 example.com 文本);无 environ 凭证读取。
- [ok] 元数据核对 — GitHub API:MIT、1385 stars、pushed 2026-09-09T12:33:29Z;本地 git HEAD == d5c4678cb5d4 == pin。
6Verdict & Scenarios
71b6589924471f21…