Python使用Docling库玩转文档处理
作者:正东AI
一、背景
在日常开发中,文档处理一直是令人头疼的问题。无论是技术文档、设计文档,还是各种格式的文件(如 PDF、DOCX、PPTX等),如何高效地解析、转换和提取信息,常常耗费大量精力。Docling 的出现,为这一问题提供了优雅的解决方案。它不仅支持多种主流文档格式,还能深度解析PDF,提取页面布局、表格结构等复杂信息。更重要的是,Docling 提供了统一的文档表示格式和便捷的 CLI,使得文档处理变得简单高效。
接下来,我们将深入了解 Docling 的强大功能,并通过实际代码示例,展示它如何帮助我们高效处理文档。
二、什么是 Docling
Docling 是一个强大的 Python 第三方库,专注于文档处理和转换。它支持多种文档格式,包括PDF、DOCX、PPTX、HTML、图片等。Docling 的核心功能是深度解析 PDF,能够识别页面布局、阅读顺序、表格结构,甚至支持 OCR功能,处理扫描版文档。此外,Docling 还提供了统一的文档表示格式(DoclingDocument),方便开发者进行后续处理。
三、安装 Docling
作为第三方库,Docling 的安装非常简单。只需通过 pip 命令即可完成安装:
pip install docling
如果需要支持 CPU 版本的 PyTorch,可以使用以下命令:
pip install docling --extra-index-url https://download.pytorch.org/whl/cpu
安装完成后,即可使用 Docling 提供的强大功能。
四、库函数使用方法
以下是 Docling 的五个常用函数及其使用方法:
1. DocumentConverter.convert()
该函数用于转换文档,支持本地路径或 URL。
from docling.document_converter import DocumentConverter source = "https://arxiv.org/pdf/2408.09869" # 文档路径或 URL converter = DocumentConverter() result = converter.convert(source)
source:文档的路径或 URL。
converter.convert():将文档转换为 Docling 的内部表示格式。
2. export_to_markdown()
将文档导出为 Markdown 格式。
markdown_content = result.document.export_to_markdown() print(markdown_content)
export_to_markdown():将文档内容转换为 Markdown 格式。
3. export_to_json()
将文档导出为 JSON 格式。
json_content = result.document.export_to_json() print(json_content)
export_to_json():将文档内容转换为 JSON 格式。
4. HierarchicalChunker.chunk()
对文档进行分块处理,返回文本内容和元数据。
from docling_core.transforms.chunker import HierarchicalChunker chunks = list(HierarchicalChunker().chunk(result.document)) print(chunks[0])
HierarchicalChunker():创建分块器。
chunk(result.document):对文档进行分块处理。
5. PdfPipelineOptions
自定义 PDF 转换选项。
from docling.datamodel.pipeline_options import PdfPipelineOptions pipeline_options = PdfPipelineOptions(do_table_structure=True) pipeline_options.table_structure_options.do_cell_matching = False
PdfPipelineOptions:自定义 PDF 转换选项。
do_table_structure:是否解析表格结构。
do_cell_matching:是否将表格单元格映射回 PDF。
五、使用场景示例
以下是五个实际使用场景及其代码示例:
场景 1:将 PDF 转换为 Markdown
from docling.document_converter import DocumentConverter source = "https://arxiv.org/pdf/2408.09869" converter = DocumentConverter() result = converter.convert(source) markdown_content = result.document.export_to_markdown() print(markdown_content)
convert():将 PDF 转换为 Docling 的内部表示格式。
export_to_markdown():将文档导出为 Markdown 格式。
场景 2:限制文档大小
from docling.document_converter import DocumentConverter source = "https://arxiv.org/pdf/2408.09869" converter = DocumentConverter() result = converter.convert(source, max_num_pages=100, max_file_size=20971520)
max_num_pages:限制文档的最大页数。
max_file_size:限制文档的最大文件大小。
场景 3:自定义 PDF 转换选项
from docling.datamodel.pipeline_options import PdfPipelineOptions from docling.document_converter import DocumentConverter pipeline_options = PdfPipelineOptions(do_table_structure=True) pipeline_options.table_structure_options.do_cell_matching = False converter = DocumentConverter(pipeline_options=pipeline_options) result = converter.convert("path/to/your/document.pdf")
PdfPipelineOptions:自定义 PDF 转换选项。
do_table_structure 和 do_cell_matching:控制表格结构的解析方式。
场景 4:文档分块处理
from docling.document_converter import DocumentConverter from docling_core.transforms.chunker import HierarchicalChunker converter = DocumentConverter() result = converter.convert("https://arxiv.org/pdf/2206.01062") chunks = list(HierarchicalChunker().chunk(result.document)) print(chunks[0])
HierarchicalChunker.chunk():对文档进行分块处理。
输出包含文本内容和元数据,方便后续处理。
场景 5:使用 OCR 处理扫描版 PDF
from docling.datamodel.pipeline_options import PipelineOptions, TesseractOcrOptions from docling.document_converter import DocumentConverter pipeline_options = PipelineOptions() pipeline_options.do_ocr = True pipeline_options.ocr_options = TesseractOcrOptions() converter = DocumentConverter(pipeline_options=pipeline_options) result = converter.convert("path/to/scanned_document.pdf")
PipelineOptions 和 TesseractOcrOptions:配置 OCR 选项。
do_ocr:启用 OCR 功能。
六、常见问题及解决方案
以下是使用 Docling 时常见的三个问题及其解决方案:
问题 1:TensorFlow 相关警告
错误信息:
This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
解决方案:安装适合 CPU 的 TensorFlow 版本。
conda create --name py11 python==3.11 conda activate py11 conda install tensorflow
问题 2:Tesseract OCR 安装问题
错误信息:Tesseract OCR 未安装或配置错误。
解决方案:安装 Tesseract OCR 并设置 TESSDATA_PREFIX。
# macOS brew install tesseract leptonica pkg-config TESSDATA_PREFIX=/opt/homebrew/share/tessdata/ # Linux apt-get install tesseract-ocr tesseract-ocr-eng libtesseract-dev libleptonica-dev pkg-config TESSDATA_PREFIX=$(dpkg -L tesseract-ocr-eng | grep tessdata$)
问题 3:Tesserocr 安装失败
错误信息:Tesserocr 安装失败。
解决方案:重新安装 Tesserocr。
pip uninstall tesserocr pip install --no-binary :all: tesserocr
七、总结
Docling是一个功能强大的文档处理库,支持多种文档格式和深度解析功能。它提供了统一的文档表示格式和丰富的导出选项,能够满足多种开发需求。通过简单的安装和使用,开发者可以轻松地将文档处理集成到自己的项目中。无论是技术文档处理还是AI 应用开发,Docling 都是一个值得信赖的选择。
到此这篇关于Python使用Docling库玩转文档处理的文章就介绍到这了,更多相关Python Docling文档处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!