Skip to main content

Universal document parser for RAG applications. Chinese-first.

Project description

DocForge

Python License: MIT

通用文档解析器,专为 RAG 应用设计。中文优先。一次函数调用将任意文档转换为结构化 Markdown。

Universal document parser for RAG applications. Chinese-first. One function call converts any document to structured Markdown.


中文 | English


中文

安装

pip install docforge                    # 核心功能
pip install docforge[ocr-surya]        # 含 Surya OCR
pip install docforge[all]              # 全部功能

快速开始

from docforge import parse

# 解析原生 PDF
result = parse("report.pdf")
print(result.markdown)

# 解析扫描件 PDF(OCR)
result = parse("scanned.pdf", ocr_backend="surya")

# 解析 DOCX 并提取图片
result = parse("document.docx", extract_images=True)

Python API

parse(file_path, **kwargs) -> ParseResult

核心函数。接收文件路径和可选配置参数,返回 ParseResult

ParseResult 包含:

  • markdown: str — 渲染好的 Markdown 文本
  • blocks: list[ContentBlock] — 通用中间表示(文本/表格/图片)
  • metadata: DocumentMetadata — 文件类型、页数、解析方式
  • tables: list[TableResult] — 表格索引(含行列数)
  • images: list[ImageResult] — 图片索引(路径、位置)
  • warnings: list[ParseWarning] — 非致命警告

ParseConfig 可选参数(通过 **kwargs 传入):

参数 默认值 说明
ocr_backend "auto" OCR 后端:"paddle" / "surya" / "auto" / "none"
ocr_languages ("chi_sim", "eng") OCR 识别语言
ocr_fallback True 原生解析质量不足时自动切换 OCR
extract_images True 提取嵌入图片
image_format "png" 图片输出格式
table_mode "accurate" 表格解析模式
dpi 200 图片/PDF 渲染 DPI
max_pages None 最大解析页数

CLI 用法

# 单文件解析
docforge parse document.pdf -o output.md

# 指定 OCR 后端
docforge parse document.pdf --ocr surya

# 导出表格为 CSV
docforge parse document.pdf --export-tables

# 批量解析目录
docforge parse ./docs/ --recursive -o ./output/

支持的格式

格式 扩展名 解析方式
PDF(原生文字层) .pdf PyMuPDF 原生提取
PDF(扫描件) .pdf OCR(Surya / PaddleOCR)
Word .docx python-docx 原生提取
Excel .xlsx openpyxl 原生提取
PowerPoint .pptx python-pptx 原生提取
图片 .png .jpg .jpeg .gif .bmp OCR
纯文本 .txt 直接读取
Markdown .md 直接读取

已知限制

  • v1 仅处理带边框表格,无边框表格不识别
  • 不支持数学公式(ContentType.FORMULA 保留但未实现)
  • 跨页表格仅做简单合并,不处理复杂跨页拆分
  • OCR 扫描件解析比原生文本慢,建议为大批量场景安装 GPU 版 OCR

参与贡献

git clone https://github.com/CalmDownTR/docforge.git
cd docforge
uv sync --extra dev
uv run pytest tests/
uv run ruff check src/

欢迎提交 Issue 和 PR。

许可证

MIT License


English

Installation

pip install docforge                    # Core only
pip install docforge[ocr-surya]        # With Surya OCR
pip install docforge[all]              # Everything

Quickstart

from docforge import parse

# Parse a native PDF
result = parse("report.pdf")
print(result.markdown)

# Parse a scanned PDF with OCR
result = parse("scanned.pdf", ocr_backend="surya")

# Parse a DOCX and extract images
result = parse("document.docx", extract_images=True)

Python API

parse(file_path, **kwargs) -> ParseResult

Core function. Takes a file path and optional config overrides, returns a ParseResult.

ParseResult fields:

  • markdown: str — rendered Markdown text
  • blocks: list[ContentBlock] — universal IR (text/table/image)
  • metadata: DocumentMetadata — file type, page count, parse method
  • tables: list[TableResult] — table index (row/col counts)
  • images: list[ImageResult] — image index (path, position)
  • warnings: list[ParseWarning] — non-fatal warnings

ParseConfig options (via **kwargs):

Parameter Default Description
ocr_backend "auto" OCR backend: "paddle" / "surya" / "auto" / "none"
ocr_languages ("chi_sim", "eng") OCR recognition languages
ocr_fallback True Auto-switch to OCR on low-quality native extraction
extract_images True Extract embedded images
image_format "png" Output image format
table_mode "accurate" Table extraction mode
dpi 200 Image/PDF render DPI
max_pages None Maximum pages to parse

CLI Usage

# Single file
docforge parse document.pdf -o output.md

# Specify OCR backend
docforge parse document.pdf --ocr surya

# Export tables as CSV
docforge parse document.pdf --export-tables

# Batch parse a directory
docforge parse ./docs/ --recursive -o ./output/

Supported Formats

Format Extension Method
PDF (native text) .pdf PyMuPDF native extraction
PDF (scanned) .pdf OCR (Surya / PaddleOCR)
Word .docx python-docx native extraction
Excel .xlsx openpyxl native extraction
PowerPoint .pptx python-pptx native extraction
Image .png .jpg .jpeg .gif .bmp OCR
Plain text .txt Direct read
Markdown .md Direct read

Known Limitations

  • v1 handles bordered tables only; borderless tables are not detected
  • Math formulas are not supported (ContentType.FORMULA is reserved but not implemented)
  • Cross-page table merging is basic; complex split tables are not handled
  • OCR parsing of scanned documents is slower than native text; consider GPU-accelerated OCR for batch processing

Contributing

git clone https://github.com/CalmDownTR/docforge.git
cd docforge
uv sync --extra dev
uv run pytest tests/
uv run ruff check src/

Issues and PRs welcome.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docforge_parser-1.0.0.tar.gz (66.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docforge_parser-1.0.0-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file docforge_parser-1.0.0.tar.gz.

File metadata

  • Download URL: docforge_parser-1.0.0.tar.gz
  • Upload date:
  • Size: 66.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for docforge_parser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4f23a606897e66081565c39eed7142ade6a068a752e2bce73668764e50103564
MD5 1e2c7c1d950b1ed6f3b0d4a0972d2791
BLAKE2b-256 53dc232a4bab9a3a13085931784ace784a938478fb98bb7aa9bedaccfbeaf3d8

See more details on using hashes here.

File details

Details for the file docforge_parser-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: docforge_parser-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for docforge_parser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb346e94fe266ae1f798df1c01ba15ec6147d228fa9f8b36ed31fd4e6cba928e
MD5 1217c2fd9006ac1a9671d3eb67554c7d
BLAKE2b-256 0082e5173ad4c97f5aebb1136a4036cc0ad922e819d81856271fb49427bb90a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page