链载Ai

标题: 超强大的PDF数据提取库PyMuPDF4LLM [打印本页]

作者: 链载Ai    时间: 1 小时前
标题: 超强大的PDF数据提取库PyMuPDF4LLM

PyMuPDF4LLM,顾名思义,它是一个为pymupdf添加了LLM的输出功能的库。

PyMuPDF4LLM的特点:

多功能 Markdown 提取

PyMuPDF4LLM支持广泛的任务,从基本的文本提取到适合微调LLM的 Markdown格式的数据采集。

灵活运用

除了文本提取之外,该工具还可以逐页检索数据,将其保存为RAG任务所需的格式,甚至将数据直接传递给 LlamaIndex 等框架。

丰富的提取功能

除了文本之外,还可以满足图像提取、分词、表格提取等细节需求。这些功能在多种情况下都很有用,包括知识库创建和情感分析。

安装方法

首先,让我们安装 PyMuPDF4LLM。

pipinstallpymupdf4llm

用例 1:基本 Markdown 提取

让我们获取 Markdown 格式的 PDF 内容。

importpymupdf4llmmd_text=pymupdf4llm.to_markdown("document.pdf")

以 Markdown 格式获取可以保留标题和列表等结构,使其成为 LLM 训练数据的理想选择。

用例 2:仅提取特定页面

仅提取您需要的页面也很容易。

importpymupdf4llm#只抽取指定的第8页,第9页md_text=pymupdf4llm.to_markdown("document.pdf",pages=[8,9])

它非常高效,因为您可以从大型PDF中仅提取所需的部分。

用例 3:将 Markdown 保存到文件

让我们将提取的 Markdown 保存到文件中。

importpymupdf4llmimportpathlibmd_text=pymupdf4llm.to_markdown("document.pdf")pathlib.Path("output.md").write_bytes(md_text.encode())

用例 4:提取为 LlamaIndex 的文档

您还可以以与 LlamaIndex兼容的格式检索数据。

importpymupdf4llmllama_reader=pymupdf4llm.LlamaMarkdownReader()llama_docs=llama_reader.load_data("document.pdf")

用例 5:提取图像

提取图像也很容易。

md_text_images=pymupdf4llm.to_markdown(doc="document.pdf",pages=[1,11],page_chunks=True,write_images=True,image_path="images",image_format="png",dpi=300)

用例 6:对数据进行分块并使用元数据进行提取

还可以对数据进行分块并使用元数据检索它。

md_text_chunks=pymupdf4llm.to_markdown(doc="document.pdf",pages=[0,1,2],page_chunks=True)

用例 7:详细的逐字提取

它还支持您想要以字为单位获取数据的情况。

md_text_words=pymupdf4llm.to_markdown(doc="document.pdf",pages=[1,2],page_chunks=True,write_images=True,image_path="images",image_format="png",dpi=300,extract_words=True)print(md_text_words[0]['words'][:5])

用法示例8:整齐地提取表格

importpymupdf4llmmd_text_tables=pymupdf4llm.to_markdown(doc="document.pdf",pages=[12]#存在表格的页面)print(md_text_tables)

如果您正在构建RAG系统、微调您的LLM,或者只是在寻找出色的 PDF提取工具,可以尝试一下PyMuPDF4LLM。







欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5