DS4SD/docling项目v2版本全面解析:文档转换与处理新特性

DS4SD/docling项目v2版本全面解析:文档转换与处理新特性

docling Get your documents ready for gen AI docling 项目地址: https://gitcode.com/gh_mirrors/do/docling

项目概述

DS4SD/docling是一个强大的文档处理工具库,专注于将各种格式的文档转换为结构化的数据表示。最新发布的v2版本带来了多项重大改进,使得文档处理能力更加强大和灵活。

v2版本核心新特性

  1. 多格式支持扩展:新增对PDF、MS Word、MS PowerPoint、HTML以及多种图像格式的解析与转换能力
  2. 统一文档表示:引入全新的通用文档数据结构,能够完整保留文档的层次结构信息
  3. API与CLI重构:提供更简洁直观的编程接口和命令行工具

命令行工具(CLI)升级详解

v2版本对命令行接口进行了全面优化,使其更加统一和强大:

# 基础文件转换示例
docling myfile.pdf  # 默认转换为Markdown格式

# 多格式输出控制
docling myfile.pdf --to json --to md --no-ocr

# 批量目录处理
docling ./input/dir --from pdf --from docx --to md --to json --output ./scratch

# 错误处理增强
docling ./input/dir --output ./scratch --abort-on-error

与v1版本的主要差异

  • 移除了独立的导出格式开关,改用--from--to参数分别控制输入输出格式
  • 新增--abort-on-error选项,可在遇到第一个错误时立即终止批量转换
  • 移除了PDF专用的--backend选项

Python API深度解析

文档转换器(DocumentConverter)配置

v2版本重构了文档转换器的初始化方式,支持更灵活的格式配置:

from docling.document_converter import DocumentConverter
from docling.datamodel.base_models import InputFormat
from docling.document_converter import PdfFormatOption, WordFormatOption

# 默认初始化方式保持不变
doc_converter = DocumentConverter()

# 高级配置示例
pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = False
pipeline_options.do_table_structure = True

doc_converter = DocumentConverter(
    allowed_formats=[InputFormat.PDF, InputFormat.DOCX],
    format_options={
        InputFormat.PDF: PdfFormatOption(
            pipeline_options=pipeline_options,
            backend=PyPdfiumDocumentBackend
        ),
        InputFormat.DOCX: WordFormatOption(
            pipeline_cls=SimplePipeline
        )
    }
)

文档转换操作优化

转换API进行了语义化重构,使用更加直观:

# 单文件转换(原convert_single)
conv_result = doc_converter.convert("file.pdf")

# 批量转换(原convert)
input_files = ["file1.pdf", "file2.docx"]
conv_results = doc_converter.convert_all(input_files)

# 错误处理控制
conv_results = doc_converter.convert_all(input_files, raises_on_error=False)

文档结构与访问机制

v2版本引入了全新的DoclingDocument数据结构,提供了更强大的文档访问能力:

# 文档结构可视化
conv_result.document.print_element_tree()

# 层次化遍历文档内容
for item, level in conv_result.document.iterate_items():
    if isinstance(item, TextItem):
        print(item.text)
    elif isinstance(item, TableItem):
        table_df = item.export_to_dataframe()

数据导出功能详解

v2版本统一了导出接口,所有导出方法都集中在DoclingDocument对象上:

# JSON导出
print(json.dumps(conv_res.document.export_to_dict()))

# Markdown导出
print(conv_res.document.export_to_markdown())

# 文档标记导出
print(conv_res.document.export_to_document_tokens())

文档持久化与加载

# 保存文档
with Path("./doc.json").open("w") as fp:
    fp.write(json.dumps(doc.export_to_dict()))

# 加载文档
with Path("./doc.json").open("r") as fp:
    doc_dict = json.loads(fp.read())
    doc = DoclingDocument.model_validate(doc_dict)

高级功能:文档分块(Chunking)

v2版本重构了分块系统,引入新的基类体系:

  • BaseMeta:分块元数据基类
  • BaseChunk:包含文本和元数据的分块基类
  • BaseChunker:分块器基类

特别提供了增强版的HierarchicalChunker实现,能够利用文档的层次结构信息生成更丰富的分块结果,包括:

  • 相关文档项作为基础
  • 适用的标题作为上下文
  • 适用的说明文字作为上下文

总结

DS4SD/docling v2版本通过统一的数据表示和简化的API设计,大幅提升了文档处理的灵活性和易用性。无论是简单的格式转换需求,还是复杂的文档分析任务,新版本都能提供更加强大和一致的解决方案。对于需要处理多种文档格式并提取结构化数据的应用场景,v2版本无疑是一个值得考虑的工具选择。

docling Get your documents ready for gen AI docling 项目地址: https://gitcode.com/gh_mirrors/do/docling

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏栋赢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值