这里是正文内容...
表格示例
| 列1 | 列2 | 列3 |
|---|---|---|
| 数据1 | 数据2 | 数据3 |
代码块示例
def example_function():
return "Hello Docling"
图片描述

### JSON结构化输出
docling的JSON输出包含丰富的结构化信息:
```json
{
"metadata": {
"title": "文档标题",
"author": "作者",
"creation_date": "2024-01-01"
},
"content": [
{
"type": "heading",
"level": 2,
"text": "章节标题",
"confidence": 0.95
},
{
"type": "paragraph",
"text": "段落内容",
"confidence": 0.92
},
{
"type": "table",
"rows": [
{"cells": ["标题1", "标题2"]},
{"cells": ["数据1", "数据2"]}
]
}
]
}
🔧 配置选项详解
管道选项配置
from docling.datamodel.pipeline_options import PipelineOptions
from docling.document_converter import DocumentConverter
# 自定义处理选项
pipeline_options = PipelineOptions()
pipeline_options.do_ocr = True # 启用OCR
pipeline_options.do_layout = True # 启用布局分析
pipeline_options.do_tables = True # 启用表格识别
converter = DocumentConverter(pipeline_options=pipeline_options)
result = converter.convert("document.pdf")
OCR引擎选择
from docling.datamodel.pipeline_options import PipelineOptions, EasyOcrOptions
pipeline_options = PipelineOptions()
pipeline_options.do_ocr = True
pipeline_options.ocr_options = EasyOcrOptions() # 使用EasyOCR引擎
converter = DocumentConverter(pipeline_options=pipeline_options)
🚨 常见问题处理
错误处理示例
from docling.document_converter import DocumentConverter
from docling.exceptions import DoclingConversionError
try:
converter = DocumentConverter()
result = converter.convert("invalid_file.txt")
print("转换成功!")
except DoclingConversionError as e:
print(f"转换错误: {e}")
except FileNotFoundError:
print("文件不存在")
except Exception as e:
print(f"未知错误: {e}")
性能优化建议
# 禁用不需要的功能提升性能
pipeline_options = PipelineOptions()
pipeline_options.do_ocr = False # 如果不是扫描文档
pipeline_options.do_layout = False # 如果不需要布局分析
pipeline_options.do_tables = False # 如果不需要表格识别
converter = DocumentConverter(pipeline_options=pipeline_options)
📊 使用场景对比
| 场景类型 | 推荐配置 | 输出格式 | 注意事项 |
|---|---|---|---|
| 简单文本提取 | 默认选项 | Markdown | 处理速度快,资源消耗低 |
| 扫描文档处理 | 启用OCR | Markdown/JSON | 需要安装Tesseract |
| 结构化数据提取 | 启用表格识别 | JSON | 保留表格结构信息 |
| 学术论文处理 | 全功能启用 | Markdown | 支持公式和参考文献 |
🎯 最佳实践总结
- 安装验证:安装后运行简单测试确认功能正常
- 格式选择:根据下游应用选择合适的输出格式
- 错误处理:添加适当的异常处理机制
- 性能调优:根据需求禁用不必要的功能模块
- 批量处理:使用CLI进行大规模文档处理
验证安装成功
# 简单测试脚本
from docling.document_converter import DocumentConverter
def test_installation():
try:
converter = DocumentConverter()
print("✅ docling安装成功!")
return True
except Exception as e:
print(f"❌ 安装存在问题: {e}")
return False
if __name__ == "__main__":
test_installation()
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



