当前位置: 代码网 > it编程>前端脚本>Python > Python使用Docling库玩转文档处理

Python使用Docling库玩转文档处理

2025年02月17日 Python 我要评论
一、背景在日常开发中,文档处理一直是令人头疼的问题。无论是技术文档、设计文档,还是各种格式的文件(如 pdf、docx、pptx等),如何高效地解析、转换和提取信息,常常耗费大量精力。docling

一、背景

在日常开发中,文档处理一直是令人头疼的问题。无论是技术文档、设计文档,还是各种格式的文件(如 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文档处理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com