Docling 实用示例

面向任务的简短配方,包含 CLI 命令、对应的 Python 代码、预期输出和常见错误。每个配方都链接到官方来源。

PDF 转 Markdown

PDF 与转换

从任何数字版 PDF 获得干净、可读的 Markdown。

适用场景: 当你需要从数字或混合 PDF 得到干净、可读的 Markdown 时。

CLI

docling convert report.pdf --to md

Python

from docling.document_converter import DocumentConverter

converter = DocumentConverter()
result = converter.convert("report.pdf")
print(result.document.export_to_markdown())

使用的标志

  • --to md 选择 Markdown 输出。

预期输出

# Annual Report

## Revenue

| Year | Revenue |
|---|---:|
| 2025 | $12M |
| 2026 | $15M |

变体

跳过 OCR 以提速

docling convert report.pdf --to md --no-ocr

常见错误: 对扫描 PDF 运行却得到空文本。如果 PDF 没有文本层,请使用 OCR(--ocr-mode full_page)。

官方文档 · PDF 转 JSON

PDF 转 JSON

PDF 与转换

用于自定义流水线的完整 DoclingDocument 结构。

适用场景: 当你需要无损的 DoclingDocument 结构,包括边界框和版面元数据时。

CLI

docling convert report.pdf --to json

Python

import json
from docling.document_converter import DocumentConverter

converter = DocumentConverter()
result = converter.convert("report.pdf")
print(json.dumps(result.document.export_to_dict(), indent=2))

使用的标志

  • --to json 导出无损 JSON 表示。

预期输出

{
  "schema_name": "DoclingDocument",
  "texts": [ ... ],
  "tables": [ ... ]
}

变体

跳过 OCR 以提速

docling convert report.pdf --to json --no-ocr

常见错误: 期望 CLI 的 JSON 与 export_to_dict() 逐字节相同;它们是同一文档的等价表示。

官方文档 · PDF 转 Markdown

URL 转 Markdown

PDF 与转换

直接从 HTTP URL 转换文档。

适用场景: 当文档在线上、你不想先下载时。

CLI

docling convert https://arxiv.org/pdf/2408.09869 --to md

Python

from docling.document_converter import DocumentConverter

converter = DocumentConverter()
result = converter.convert("https://arxiv.org/pdf/2408.09869")
print(result.document.export_to_markdown())

预期输出

## Docling Technical Report

...

变体

改为导出 JSON

docling convert https://arxiv.org/pdf/2408.09869 --to json

常见错误: 认为任何 URL 都能用。源必须是可通过 HTTP 访问的受支持文档格式。

官方文档 · 支持的格式

带 OCR 的扫描 PDF

OCR 与扫描

从纯图片页面恢复文本。

适用场景: 当页面是图像、没有可选文本时。

CLI

docling convert scan.pdf --ocr-mode full_page

Python

from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions

pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = True

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("scan.pdf")
print(result.document.export_to_markdown())

使用的标志

  • --ocr-mode full_page 对每一页执行 OCR,即使已有文本。

预期输出

Text reconstructed from the scanned page image.

变体

选择其他引擎

docling convert scan.pdf --ocr-mode full_page --ocr-engine rapidocr

常见错误: 对数字 PDF 一直开启 OCR 会浪费时间。仅在缺少文本层时启用。

官方文档 · 对比 OCR 引擎

为数字 PDF 禁用 OCR

OCR 与扫描

数字版 PDF 的转换速度大幅提升。

适用场景: 当 PDF 已有文本层、你想要最快转换时。

CLI

docling convert report.pdf --no-ocr --to md

Python

from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions

pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = False

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("report.pdf")
print(result.document.export_to_markdown())

使用的标志

  • --no-ocr 完全跳过 OCR 阶段。

预期输出

Markdown produced without running OCR.

变体

同时使用快速表格

docling convert report.pdf --no-ocr --table-mode fast

常见错误: 对扫描 PDF 禁用 OCR 会几乎得不到文本。

官方文档 · 所有命令

提取表格

表格

将表格转换为 Markdown 或 HTML 矩阵。

适用场景: 当文档包含你想以 Markdown 或 HTML 矩阵形式获取的表格时。

CLI

docling convert report.pdf --to md

Python

from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions

pipeline_options = PdfPipelineOptions()
pipeline_options.do_table_structure = True

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("report.pdf")
print(result.document.export_to_markdown())

使用的标志

  • --table-mode accurate 对复杂合并单元格表格使用 TableFormer。

预期输出

| Region | Q1 | Q2 |
|---|---:|---:|
| EMEA | 4.2 | 4.8 |

变体

更快的近似表格

docling convert report.pdf --to md --table-mode fast

常见错误: 认为每个表格都完美。合并单元格和无边框表格可能仍需人工检查;可尝试 --table-mode accurate。

官方文档 · 构建命令

提取公式与代码

公式与代码

将公式和代码块提取为 LaTeX。

适用场景: 用于含公式或代码块的科研或技术文档。

CLI

docling convert paper.pdf --enrich-code --enrich-formula

Python

from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions

pipeline_options = PdfPipelineOptions()
pipeline_options.do_code_enrichment = True
pipeline_options.do_formula_enrichment = True

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("paper.pdf")
print(result.document.export_to_markdown())

使用的标志

  • --enrich-code 检测代码块。
  • --enrich-formula 提取 LaTeX 公式。

预期输出

$$ E = mc^2 $$

变体

添加图表提取

docling convert paper.pdf --enrich-code --enrich-formula --enrich-chart-extraction

常见错误: 对没有公式或代码的文档启用增强只会增加处理时间而收益很小。

官方文档 · 构建命令

使用 HybridChunker 进行 RAG 分块

RAG

适用于向量库的结构感知分块。

适用场景: 当你想要保留标题、表格和页面元数据以供向量库使用的分块时。

CLI

docling convert report.pdf --to chunks --chunks-type hybrid

Python

from docling.document_converter import DocumentConverter
from docling.chunking import HybridChunker

converter = DocumentConverter()
result = converter.convert("report.pdf")

chunker = HybridChunker()
for chunk in chunker.chunk(result.document):
    print(chunk.text)

使用的标志

  • --to chunks 输出分块结果。
  • --chunks-type hybrid 使用 HybridChunker。

预期输出

Chunks split on document structure rather than raw character counts.

变体

查看原始导出

docling convert report.pdf --to json

常见错误: 使用简单的字符分割而非结构感知分块,会破坏表格和标题。

官方文档 · RAG 指南

LangChain 集成

集成

将解析后的文档加载到 LangChain 流水线。

适用场景: 当你将解析后的文档载入 LangChain 流水线时。

CLI

pip install langchain-docling

Python

from langchain_docling import DoclingLoader

loader = DoclingLoader(file_path="report.pdf")
docs = loader.load()
print(docs[0].page_content[:200])

预期输出

LangChain Document objects with parsed page content and metadata.

变体

然后转换文件

docling convert report.pdf --to md

常见错误: 忘记单独安装与 docling 分离的集成包。

官方文档 · LlamaIndex 示例

LlamaIndex 集成

集成

从解析后的文件构建 LlamaIndex 节点。

适用场景: 当你从解析后的文件构建 LlamaIndex 文档节点时。

CLI

pip install llama-index-readers-docling

Python

from llama_index.readers.docling import DoclingReader

reader = DoclingReader()
documents = reader.load_data(file_path="report.pdf")
print(documents[0].text[:200])

预期输出

LlamaIndex documents ready for indexing.

变体

然后转换文件

docling convert report.pdf --to md

常见错误: 混用 docling 与 reader 包的版本;请让两者都保持最新。

官方文档 · LangChain 示例

1
?? 1

如何使用这些示例

十个可直接复制的配方,覆盖最常见的 Docling 任务。每个配方都包含 CLI 命令、Python 等效代码、所用的参数、预期输出以及要避免的错误。

从上到下依次操作每个配方。

  1. 找到你的任务 按类别筛选,或搜索参数或关键词。
  2. 复制命令 使用 CLI 块上的复制按钮,或复制 Python 等效代码。
  3. 运行它 默认情况下,命令会把转换后的文件写在源文件旁边。
  4. 检查输出 与配方中显示的预期输出进行对比。
  5. 灵活调整 添加配方中的参数,或在配置生成器中构建自定义命令。
2
?? 2

开始之前

每个示例都假定已安装 Docling 并准备好一个示例文档。

  • 需要 Python 3.10 或更高版本。
  • OCR 示例需要 OCR 引擎;RapidOCR 是适合 CPU 的默认选择。
  • 集成示例会安装单独的软件包。
pip install docling
docling --help
3
?? 3

我该用哪个示例?

选择最接近你任务的一行。

我想要…示例关键选项
获得可读文本PDF 转 Markdown--to md
获得结构化数据PDF 转 JSON--to json
转换 URLURL 转 MarkdownURL 参数
读取扫描件或照片扫描 PDF 与 OCR--ocr-mode full_page
加快数字版 PDF关闭 OCR--no-ocr
提取表格提取表格--table-mode
获得公式和代码提取公式和代码--enrich-*
为 RAG 分块RAG 分块--to chunks
使用 LangChainLangChain 集成集成包
使用 LlamaIndexLlamaIndex 集成集成包
4
?? 4

如何调整配方

只需少量改动即可调整任何配方。

  • 将文件名或 URL 替换为你的源。
  • 数字版 PDF 添加 --no-ocr,扫描件添加 --ocr-mode full_page
  • --to md--to json--to html--to doctags 切换输出。
  • 大批量处理时添加 --device cuda--num-threads
  • 配置生成器中构建完整命令。
5
?? 5

CLI 还是 Python?

一次性转换用 CLI,需要对文档进行后处理、批量处理多个文件或与其他库集成时用 Python API。

  • CLI:快速、可脚本化,无需写代码。
  • Python:完全访问 DoclingDocument、分块和流水线选项。
6
?? 6

继续探索

相关工具与参考。

7
?? 7

常见问题

这些示例可以直接运行吗?
可以,只要安装了 Docling。把示例文件名(report.pdf、scan.pdf、paper.pdf)换成你自己的文件即可。
转换后的文件保存在哪里?
默认保存在源文件旁边,扩展名与输出格式一致。
可以一次处理多个文件吗?
可以。向 CLI 传入多个路径,或在 Python 中循环处理文件。
如何在 CLI 和 Python 之间选择?
快速转换用 CLI,需要以编程方式处理结果时用 Python。
为什么我的扫描 PDF 是空的?
它没有文本层。请使用带 --ocr-mode full_page 的 OCR 配方。
如何获得用于向量数据库的分块?
使用 RAG 分块配方,然后把分块传给向量库。请参阅 RAG 指南