Python使用Docling庫(kù)玩轉(zhuǎn)文檔處理
一、背景
在日常開(kāi)發(fā)中,文檔處理一直是令人頭疼的問(wèn)題。無(wú)論是技術(shù)文檔、設(shè)計(jì)文檔,還是各種格式的文件(如 PDF、DOCX、PPTX等),如何高效地解析、轉(zhuǎn)換和提取信息,常常耗費(fèi)大量精力。Docling 的出現(xiàn),為這一問(wèn)題提供了優(yōu)雅的解決方案。它不僅支持多種主流文檔格式,還能深度解析PDF,提取頁(yè)面布局、表格結(jié)構(gòu)等復(fù)雜信息。更重要的是,Docling 提供了統(tǒng)一的文檔表示格式和便捷的 CLI,使得文檔處理變得簡(jiǎn)單高效。
接下來(lái),我們將深入了解 Docling 的強(qiáng)大功能,并通過(guò)實(shí)際代碼示例,展示它如何幫助我們高效處理文檔。
二、什么是 Docling
Docling 是一個(gè)強(qiáng)大的 Python 第三方庫(kù),專(zhuān)注于文檔處理和轉(zhuǎn)換。它支持多種文檔格式,包括PDF、DOCX、PPTX、HTML、圖片等。Docling 的核心功能是深度解析 PDF,能夠識(shí)別頁(yè)面布局、閱讀順序、表格結(jié)構(gòu),甚至支持 OCR功能,處理掃描版文檔。此外,Docling 還提供了統(tǒng)一的文檔表示格式(DoclingDocument),方便開(kāi)發(fā)者進(jìn)行后續(xù)處理。
三、安裝 Docling
作為第三方庫(kù),Docling 的安裝非常簡(jiǎn)單。只需通過(guò) pip 命令即可完成安裝:
pip install docling
如果需要支持 CPU 版本的 PyTorch,可以使用以下命令:
pip install docling --extra-index-url https://download.pytorch.org/whl/cpu
安裝完成后,即可使用 Docling 提供的強(qiáng)大功能。
四、庫(kù)函數(shù)使用方法
以下是 Docling 的五個(gè)常用函數(shù)及其使用方法:
1. DocumentConverter.convert()
該函數(shù)用于轉(zhuǎn)換文檔,支持本地路徑或 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():將文檔轉(zhuǎn)換為 Docling 的內(nèi)部表示格式。
2. export_to_markdown()
將文檔導(dǎo)出為 Markdown 格式。
markdown_content = result.document.export_to_markdown() print(markdown_content)
export_to_markdown():將文檔內(nèi)容轉(zhuǎn)換為 Markdown 格式。
3. export_to_json()
將文檔導(dǎo)出為 JSON 格式。
json_content = result.document.export_to_json() print(json_content)
export_to_json():將文檔內(nèi)容轉(zhuǎn)換為 JSON 格式。
4. HierarchicalChunker.chunk()
對(duì)文檔進(jìn)行分塊處理,返回文本內(nèi)容和元數(shù)據(jù)。
from docling_core.transforms.chunker import HierarchicalChunker chunks = list(HierarchicalChunker().chunk(result.document)) print(chunks[0])
HierarchicalChunker():創(chuàng)建分塊器。
chunk(result.document):對(duì)文檔進(jìn)行分塊處理。
5. PdfPipelineOptions
自定義 PDF 轉(zhuǎn)換選項(xiàng)。
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 轉(zhuǎn)換選項(xiàng)。
do_table_structure:是否解析表格結(jié)構(gòu)。
do_cell_matching:是否將表格單元格映射回 PDF。
五、使用場(chǎng)景示例
以下是五個(gè)實(shí)際使用場(chǎng)景及其代碼示例:
場(chǎng)景 1:將 PDF 轉(zhuǎn)換為 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 轉(zhuǎn)換為 Docling 的內(nèi)部表示格式。
export_to_markdown():將文檔導(dǎo)出為 Markdown 格式。
場(chǎng)景 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:限制文檔的最大頁(yè)數(shù)。
max_file_size:限制文檔的最大文件大小。
場(chǎng)景 3:自定義 PDF 轉(zhuǎn)換選項(xiàng)
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 轉(zhuǎn)換選項(xiàng)。
do_table_structure 和 do_cell_matching:控制表格結(jié)構(gòu)的解析方式。
場(chǎng)景 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():對(duì)文檔進(jìn)行分塊處理。
輸出包含文本內(nèi)容和元數(shù)據(jù),方便后續(xù)處理。
場(chǎng)景 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 選項(xiàng)。
do_ocr:?jiǎn)⒂?OCR 功能。
六、常見(jiàn)問(wèn)題及解決方案
以下是使用 Docling 時(shí)常見(jiàn)的三個(gè)問(wèn)題及其解決方案:
問(wèn)題 1:TensorFlow 相關(guān)警告
錯(cuò)誤信息:
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
問(wèn)題 2:Tesseract OCR 安裝問(wèn)題
錯(cuò)誤信息:Tesseract OCR 未安裝或配置錯(cuò)誤。
解決方案:安裝 Tesseract OCR 并設(shè)置 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$)
問(wèn)題 3:Tesserocr 安裝失敗
錯(cuò)誤信息:Tesserocr 安裝失敗。
解決方案:重新安裝 Tesserocr。
pip uninstall tesserocr pip install --no-binary :all: tesserocr
七、總結(jié)
Docling是一個(gè)功能強(qiáng)大的文檔處理庫(kù),支持多種文檔格式和深度解析功能。它提供了統(tǒng)一的文檔表示格式和豐富的導(dǎo)出選項(xiàng),能夠滿(mǎn)足多種開(kāi)發(fā)需求。通過(guò)簡(jiǎn)單的安裝和使用,開(kāi)發(fā)者可以輕松地將文檔處理集成到自己的項(xiàng)目中。無(wú)論是技術(shù)文檔處理還是AI 應(yīng)用開(kāi)發(fā),Docling 都是一個(gè)值得信賴(lài)的選擇。
到此這篇關(guān)于Python使用Docling庫(kù)玩轉(zhuǎn)文檔處理的文章就介紹到這了,更多相關(guān)Python Docling文檔處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
wxPython:python首選的GUI庫(kù)實(shí)例分享
wxPython是Python語(yǔ)言的一套優(yōu)秀的GUI圖形庫(kù)。允許Python程序員很方便的創(chuàng)建完整的、功能鍵全的GUI用戶(hù)界面。 wxPython是作為優(yōu)秀的跨平臺(tái)GUI庫(kù)wxWidgets的Python封裝和Python模塊的方式提供給用戶(hù)的2019-10-10python 實(shí)現(xiàn)字符串下標(biāo)的輸出功能
這篇文章主要介紹了python 簡(jiǎn)單的實(shí)現(xiàn)字符串下標(biāo)的輸出,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python使用ConfigParser模塊操作配置文件的方法
這篇文章主要介紹了Python使用ConfigParser模塊操作配置文件的方法,結(jié)合實(shí)例形式分析了Python基于ConfigParser模塊針對(duì)配置文件的創(chuàng)建、讀取、寫(xiě)入、判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06Python報(bào)錯(cuò)ValueError:?cannot?convert?float?NaN?to?intege
在Python編程中,我們經(jīng)常需要處理各種數(shù)據(jù)類(lèi)型,包括浮點(diǎn)數(shù)和整數(shù),然而,有時(shí)候我們可能會(huì)遇到一些意外的情況,比如將一個(gè)包含NaN(Not?a?Number)的浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù)時(shí),就會(huì)拋出錯(cuò)誤,本文將探討這個(gè)錯(cuò)誤的原因,并給出幾種可能的解決方案,需要的朋友可以參考下2024-09-09Python替換NumPy數(shù)組中大于某個(gè)值的所有元素實(shí)例
這篇文章主要介紹了Python替換NumPy數(shù)組中大于某個(gè)值的所有元素實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Pytest如何使用skip跳過(guò)執(zhí)行測(cè)試
這篇文章主要介紹了Pytest如何使用skip跳過(guò)執(zhí)行測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08