百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

Docling 文档提取开发库

liuian 2025-03-04 13:06 44 浏览

Docling是IBM 深度搜索团队的 MIT 许可文档提取 Python 库,可轻松快速地解析文档并将其导出为所需格式。

Docling的功能包括:

  • 读取流行的文档格式(PDF、DOCX、PPTX、图像、HTML、AsciiDoc、Markdown)并导出为 Markdown 和 JSON
  • 高级 PDF 文档理解,包括页面布局、阅读顺序和表格结构
  • 统一、富有表现力的 DoclingDocument 表示格式
  • 元数据提取,包括标题、作者、参考文献和语言
  • 无缝 LlamaIndex 和 LangChain 集成,打造强大的 RAG / QA 应用程序
  • 扫描 PDF 的 OCR 支持
  • 简单方便的 CLI

1、Docling安装

要使用 Docling,只需从 Python 包管理器安装 docling,例如 pip:

pip install docling

以上命令适用于 macOS、Linux 和 Windows,支持 x86_64 和 arm64 架构。

如果你希望开发 Docling 功能、错误修复等,请从本地克隆的根目录按如下方式安装:

poetry install --all-extras

2、用docling转换单个文档

要转换单个 PDF 文档,请使用 convert(),例如:

from docling.document_converter import DocumentConverter

source = "https://arxiv.org/pdf/2408.09869"  # PDF path or URL
converter = DocumentConverter()
result = converter.convert(source)
print(result.document.export_to_markdown())  # output: "### Docling Technical Report[...]"

你还可以直接从命令行使用 Docling 转换单个文件(无论是本地文件还是通过 URL)或整个目录。

一个简单的示例如下所示:

docling https://arxiv.org/pdf/2206.01062

要查看所有可用选项(导出格式等),请运行 docling --help。

3、高级选项

示例文件 custom_convert.py 包含多种调整转换管道和功能的方法。

3.1 控制 PDF 表格提取选项

你可以控制表格结构识别是否应将识别的结构映射回 PDF 单元格(默认)或使用结构预测本身的文本单元格。如果你发现提取的表格中的多列被错误地合并为一列,这可以提高输出质量。

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

pipeline_options = PdfPipelineOptions(do_table_structure=True)
pipeline_options.table_structure_options.do_cell_matching = False  # uses text cells predicted from table structure model

doc_converter = DocumentConverter(
    format_options={
        InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
    }
)

自 docling 1.16.0 起:您可以控制要使用的 TableFormer 模式。在 TableFormerMode.FAST(默认)和 TableFormerMode.ACCURATE(更好,但更慢)之间进行选择,以获得更高质量的复杂表格结构。

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

pipeline_options = PdfPipelineOptions(do_table_structure=True)
pipeline_options.table_structure_options.mode = TableFormerMode.ACCURATE  # use more accurate TableFormer model

doc_converter = DocumentConverter(
    format_options={
        InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
    }
)

3.2 提供特定的工件路径

默认情况下,模型等工件在首次使用时会自动下载。如果您你望使用已明确预取工件的本地路径,则可以按如下方式执行:

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

# # to explicitly prefetch:
# artifacts_path = StandardPdfPipeline.download_models_hf()

artifacts_path = "/local/path/to/artifacts"

pipeline_options = PdfPipelineOptions(artifacts_path=artifacts_path)
doc_converter = DocumentConverter(
    format_options={
        InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
    }
)

3.3 对文档大小施加限制

你可以限制每个文档应允许处理的文件大小和页数:

from pathlib import Path
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)

3.4 从二进制 PDF 流转换

你可以从二进制流而不是文件系统转换 PDF,如下所示:

from io import BytesIO
from docling.datamodel.base_models import DocumentStream
from docling.document_converter import DocumentConverter

buf = BytesIO(your_binary_stream)
source = DocumentStream(filename="my_doc.pdf", stream=buf)
converter = DocumentConverter()
result = converter.convert(source)

3.5 限制资源使用量

你可以通过相应地设置环境变量 OMP_NUM_THREADS 来限制 Docling 使用的 CPU 线程。默认设置是使用 4 个 CPU 线程。

4、Docling文档分块

你可以按如下方式对 Docling 文档执行层次结构感知分块:

from docling.document_converter import DocumentConverter
from docling_core.transforms.chunker import HierarchicalChunker

conv_res = DocumentConverter().convert("https://arxiv.org/pdf/2206.01062")
doc = conv_res.document
chunks = list(HierarchicalChunker().chunk(doc))

print(chunks[30])
# {
#   "text": "Lately, new types of ML models for document-layout analysis have emerged [...]",
#   "meta": {
#     "doc_items": [{
#       "self_ref": "#/texts/40",
#       "label": "text",
#       "prov": [{
#         "page_no": 2,
#         "bbox": {"l": 317.06, "t": 325.81, "r": 559.18, "b": 239.97, ...},
#       }]
#     }],
#     "headings": ["2 RELATED WORK"],
#   }
# }

5、Docling v2

Docling v2 引入了几个新功能:

  • 理解并转换 PDF、MS Word、MS Powerpoint、HTML 和多种图像格式
  • 生成一种新的通用文档表示,可以封装文档层次结构
  • 附带全新的 API 和 CLI

5.1 CLI变化

我们更新了 Docling v2 的命令行语法以支持多种格式。示例如下。

# Convert a single file to Markdown (default)
docling myfile.pdf

# Convert a single file to Markdown and JSON, without OCR
docling myfile.pdf --to json --to md --no-ocr

# Convert PDF files in input directory to Markdown (default)
docling ./input/dir --from pdf

# Convert PDF and Word files in input directory to Markdown and JSON
docling ./input/dir --from pdf --from docx --to md --to json --output ./scratch

# Convert all supported files in input directory to Markdown, but abort on first error
docling ./input/dir --output ./scratch --abort-on-error

与 Docling v1 相比的显著变化:

  • 删除了用于不同导出格式的独立开关,并用 --from 和 --to 参数替换,分别定义输入和输出格式。
  • 新的 --abort-on-error 将在遇到错误时立即中止任何批量转换
  • PDF 的 --backend 选项已被删除

5.2 设置 DocumentConverter

为了适应多种输入格式,我们改变了设置 DocumentConverter 对象的方式。您现在可以在 DocumentConverter 初始化时定义允许的格式列表,并根据需要为每种格式指定自定义选项。默认情况下,允许所有支持的格式。如果您不提供 format_options,则将对所有 allowed_formats 使用默认值。

格式选项可以包括要使用的管道类、要提供给管道的选项以及文档后端。它们以格式特定的类型提供,例如 PdfFormatOption 或 WordFormatOption,如下所示。

from docling.document_converter import DocumentConverter
from docling.datamodel.base_models import InputFormat
from docling.document_converter import (
    DocumentConverter,
    PdfFormatOption,
    WordFormatOption,
)
from docling.pipeline.simple_pipeline import SimplePipeline
from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend

## Default initialization still works as before:
# doc_converter = DocumentConverter()


# previous `PipelineOptions` is now `PdfPipelineOptions`
pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = False
pipeline_options.do_table_structure = True
#...

## Custom options are now defined per format.
doc_converter = (
    DocumentConverter(  # all of the below is optional, has internal defaults.
        allowed_formats=[
            InputFormat.PDF,
            InputFormat.IMAGE,
            InputFormat.DOCX,
            InputFormat.HTML,
            InputFormat.PPTX,
        ],  # whitelist formats, non-matching files are ignored.
        format_options={
            InputFormat.PDF: PdfFormatOption(
                pipeline_options=pipeline_options, # pipeline options go here.
                backend=PyPdfiumDocumentBackend # optional: pick an alternative backend
            ),
            InputFormat.DOCX: WordFormatOption(
                pipeline_cls=SimplePipeline # default for office formats and HTML
            ),
        },
    )
)

注意:如果你仅使用默认值,则所有内容与 Docling v1 中的内容相同。

以下示例单元显示了更多选项:

  • run_with_formats.py
  • custom_convert.py

5.3 转换文档

我们简化了向 DocumentConverter 输入输入的方式,并重命名了转换方法以获得更好的语义。现在,您可以直接使用单个文件、输入文件列表或 DocumentStream 对象调用转换,而无需先构造 DocumentConversionInput 对象。

  • DocumentConverter.convert 现在转换单个文件输入(以前为 DocumentConverter.convert_single)。
  • DocumentConverter.convert_all 现在可以一次转换多个文件(以前为 DocumentConverter.convert)。
...
from docling.datamodel.document import ConversionResult
## Convert a single file (from URL or local path)
conv_result: ConversionResult = doc_converter.convert("https://arxiv.org/pdf/2408.09869") # previously `convert_single`

## Convert several files at once:

input_files = [
    "tests/data/wiki_duck.html",
    "tests/data/word_sample.docx",
    "tests/data/lorem_ipsum.docx",
    "tests/data/powerpoint_sample.pptx",
    "tests/data/2305.03393v1-pg9-img.png",
    "tests/data/2206.01062.pdf",
]

# Directly pass list of files or streams to `convert_all`
conv_results_iter = doc_converter.convert_all(input_files) # previously `convert`

通过 raises_on_error 参数,您还可以控制转换是否在第一次遇到问题时引发异常,或者首先弹性地转换所有文件并在每个文件中反映错误

...
conv_results_iter = doc_converter.convert_all(input_files, raises_on_error=False) # previously `convert`

5.4 访问文档结构

我们还简化了访问和导出转换后的文档数据的方式。我们的通用文档表示现在可作为 DoclingDocument 对象在转换结果中使用。DoclingDocument 提供了一组简洁的 API 来构建、迭代和导出文档中的内容,如下所示。

conv_result: ConversionResult = doc_converter.convert("https://arxiv.org/pdf/2408.09869") # previously `convert_single`

## Inspect the converted document:
conv_result.document.print_element_tree()

## Iterate the elements in reading order, including hierachy level:
for item, level in conv_result.document.iterate_items:
    if isinstance(item, TextItem):
        print(item.text)
    elif isinstance(item, TableItem):
        table_df: pd.DataFrame = item.export_to_dataframe()
        print(table_df.to_markdown())
    elif ...:
        #...

注意:虽然它已被弃用,但您仍然可以使用 Docling v1 文档表示,它可用作:

conv_result.legacy_document # provides the representation in previous ExportedCCSDocument type

5.5 导出为 JSON、Markdown、Doctags

注意:ConversionResult 中的所有 render_... 方法已在 Docling v2 中删除,现在可在 DoclingDocument 上使用:

  • DoclingDocument.export_to_dict
  • DoclingDocument.export_to_markdown
  • DoclingDocument.export_to_document_tokens
conv_result: ConversionResult = doc_converter.convert("https://arxiv.org/pdf/2408.09869") # previously `convert_single`

## Export to desired format:
print(json.dumps(conv_res.document.export_to_dict()))
print(conv_res.document.export_to_markdown())
print(conv_res.document.export_to_document_tokens())

注意:虽然它已被弃用,但您仍然可以导出 Docling v1 JSON 格式。这可通过与 DoclingDocument 类型相同的方法获得:

## Export legacy document representation to desired format, for v1 compatibility:
print(json.dumps(conv_res.legacy_document.export_to_dict()))
print(conv_res.legacy_document.export_to_markdown())
print(conv_res.legacy_document.export_to_document_tokens())

5.6 重新加载存储为 JSON 的 DoclingDocument

您可以使用以下代码以 JSON 格式将 DoclingDocument 保存并重新加载到磁盘:

# Save to disk:
doc: DoclingDocument = conv_res.document # produced from conversion result...

with Path("./doc.json").open("w") as fp:
    fp.write(json.dumps(doc.export_to_dict())) # use `export_to_dict` to ensure consistency

# Load from disk:
with Path("./doc.json").open("r") as fp:
    doc_dict = json.loads(fp.read())
    doc = DoclingDocument.model_validate(doc_dict) # use standard pydantic API to populate doc

5.7 分块

Docling v2 为分块定义了新的基类:

  • BaseMeta 用于分块元数据
  • BaseChunk 包含分块文本和元数据,以及
  • BaseChunker 用于分块器,从 DoclingDocument 中生成分块。

此外,它还提供了更新的 HierarchicalChunker 实现,它利用新的 DoclingDocument 并提供一种新的、更丰富的分块输出格式,包括:

  • 用于基础的各个文档项
  • 任何适用于上下文的标题
  • 任何适用于上下文的说明

有关示例,请查看分块用法。


原文链接:Docling 文档提取简明教程 - 汇智网

相关推荐

教你把多个视频合并成一个视频的方法

一.情况介绍当你有一个m3u8文件和一个目录,目录中有连续的视频片段,这些片段可以连成一段完整的视频。m3u8文件打开后像这样:m3u8文件,可以理解为播放列表,里面是播放视频片段的顺序。视频片段像这...

零代码编程:用kimichat合并一个文件夹下的多个文件

一个文件夹里面有很多个srt字幕文件,如何借助kimichat来自动批量合并呢?在kimichat对话框中输入提示词:你是一个Python编程专家,完成如下的编程任务:这个文件夹:D:\downloa...

Java APT_java APT 生成代码

JavaAPT(AnnotationProcessingTool)是一种在Java编译阶段处理注解的工具。APT会在编译阶段扫描源代码中的注解,并根据这些注解生成代码、资源文件或其他输出,...

Unit Runtime:一键运行 AI 生成的代码,或许将成为你的复制 + 粘贴神器

在我们构建了UnitMesh架构之后,以及对应的demo之后,便着手于实现UnitMesh架构。于是,我们就继续开始UnitRuntime,以用于直接运行AI生成的代码。PS:...

挣脱臃肿的枷锁:为什么说Vert.x是Java开发者手中的一柄利剑?

如果你是一名Java开发者,那么你的职业生涯几乎无法避开Spring。它如同一位德高望重的老国王,统治着企业级应用开发的大片疆土。SpringBoot的约定大于配置、SpringCloud的微服务...

五年后,谷歌还在全力以赴发展 Kotlin

作者|FredericLardinois译者|Sambodhi策划|Tina自2017年谷歌I/O全球开发者大会上,谷歌首次宣布将Kotlin(JetBrains开发的Ja...

kotlin和java开发哪个好,优缺点对比

Kotlin和Java都是常见的编程语言,它们有各自的优缺点。Kotlin的优点:简洁:Kotlin程序相对于Java程序更简洁,可以减少代码量。安全:Kotlin在类型系统和空值安全...

移动端架构模式全景解析:从MVC到MVVM,如何选择最佳设计方案?

掌握不同架构模式的精髓,是构建可维护、可测试且高效移动应用的关键。在移动应用开发中,选择合适的软件架构模式对项目的可维护性、可测试性和团队协作效率至关重要。随着应用复杂度的增加,一个良好的架构能够帮助...

颜值非常高的XShell替代工具Termora,不一样的使用体验!

Termora是一款面向开发者和运维人员的跨平台SSH终端与文件管理工具,支持Windows、macOS及Linux系统,通过一体化界面简化远程服务器管理流程。其核心定位是解决多平台环境下远程连接、文...

预处理的底层原理和预处理编译运行异常的解决方案

若文章对您有帮助,欢迎关注程序员小迷。助您在编程路上越走越好![Mac-10.7.1LionIntel-based]Q:预处理到底干了什么事情?A:预处理,顾名思义,预先做的处理。源代码中...

为“架构”再建个模:如何用代码描述软件架构?

在架构治理平台ArchGuard中,为了实现对架构的治理,我们需要代码+模型描述所要处理的内容和数据。所以,在ArchGuard中,我们有了代码的模型、依赖的模型、变更的模型等,剩下的两个...

深度解析:Google Gemma 3n —— 移动优先的轻量多模态大模型

2025年6月,Google正式发布了Gemma3n,这是一款能够在2GB内存环境下运行的轻量级多模态大模型。它延续了Gemma家族的开源基因,同时在架构设计上大幅优化,目标是让...

比分网开发技术栈与功能详解_比分网有哪些

一、核心功能模块一个基本的比分网通常包含以下模块:首页/总览实时比分看板:滚动展示所有正在进行的比赛,包含比分、比赛时间、红黄牌等关键信息。热门赛事/焦点战:突出显示重要的、关注度高的比赛。赛事导航...

设计模式之-生成器_一键生成设计

一、【概念定义】——“分步构建复杂对象,隐藏创建细节”生成器模式(BuilderPattern):一种“分步构建型”创建型设计模式,它将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建...

构建第一个 Kotlin Android 应用_kotlin简介

第一步:安装AndroidStudio(推荐IDE)AndroidStudio是官方推荐的Android开发集成开发环境(IDE),内置对Kotlin的完整支持。1.下载And...