PyMuPDF4LLM#
PyMuPDF4LLM 旨在让您更轻松地以 LLM 和 RAG 环境所需的格式提取 PDF 内容。它支持 Markdown 提取 以及 LlamaIndex 文档输出。
重要
您可以通过 将 PyMuPDF Pro 与 PyMuPDF4LLM 结合使用 扩展支持的文件类型,以包括 Office 文档格式 (DOC/DOCX、XLS/XLSX、PPT/PPTX、HWP/HWPX)。
PyMuPDF4LLM is aimed to make it easier to extract PDF content in the format you need for LLM & RAG environments. It supports Markdown extraction as well as LlamaIndex document output.
重要
You can extend the supported file types to also include Office document formats (DOC/DOCX, XLS/XLSX, PPT/PPTX, HWP/HWPX) by using PyMuPDF Pro with PyMuPDF4LLM.
功能#
Features
支持多列页面
支持图像和矢量图形提取(以及在 MD 文本中包含参考资料)
支持页面分块输出。
直接支持输出为 LlamaIndex Documents。
Support for multi-column pages
Support for image and vector graphics extraction (and inclusion of references in the MD text)
Support for page chunking output.
Direct support for output as LlamaIndex Documents.
功能#
Functionality
此包使用 PyMuPDF 将文件页面转换为 Markdown 格式的文本。
检测标准文本和表格,将其按正确的阅读顺序引入,然后一起转换为与 GitHub 兼容的 Markdown 文本。
通过字体大小识别标题行,并在其前适当添加一个或多个
#
标签。检测粗体、斜体、等宽文本和代码块并进行相应格式化。有序和无序列表也类似。
默认情况下,处理所有文档页面。如果需要,可以通过提供基于
0
的页码列表来指定页面子集。
This package converts the pages of a file to text in Markdown format using PyMuPDF.
Standard text and tables are detected, brought in the right reading sequence and then together converted to GitHub-compatible Markdown text.
Header lines are identified via the font size and appropriately prefixed with one or more
#
tags.Bold, italic, mono-spaced text and code blocks are detected and formatted accordingly. Similar applies to ordered and unordered lists.
By default, all document pages are processed. If desired, a subset of pages can be specified by providing a list of
0
-based page numbers.
安装#
Installation
通过 pip 安装包:
Install the package via pip with:
pip install pymupdf4llm
将文件提取为 Markdown#
Extracting a file as Markdown
要在 Markdown 中检索文档内容,只需安装该包,然后使用几行 Python 代码即可获得结果。
然后在您的 Python 脚本中执行以下操作:
import pymupdf4llm
md_text = pymupdf4llm.to_markdown("input.pdf")
备注
除了上述文件名字符串外,还可以提供 PyMuPDF Document。第二个参数可能是基于 0
的页码列表,例如 [0,1]
只会选择文档的第一页和第二页。
如果您想存储 Markdown 文件,例如存储为 UTF8 编码文件,请执行以下操作:
To retrieve your document content in Markdown simply install the package and then use a couple of lines of Python code to get results.
Then in your Python script do:
import pymupdf4llm
md_text = pymupdf4llm.to_markdown("input.pdf")
备注
Instead of the filename string as above, one can also provide a PyMuPDF Document. A second parameter may be a list of 0
-based page numbers, e.g. [0,1]
would just select the first and second pages of the document.
If you want to store your Markdown file, e.g. store as a UTF8-encoded file, then do:
import pathlib
pathlib.Path("output.md").write_bytes(md_text.encode())
将文件提取为 LlamaIndex 文档#
Extracting a file as a LlamaIndex document
PyMuPDF4LLM 支持直接转换为 LLamaIndex 文档。首先将文档转换为 Markdown 格式,然后返回 LlamaIndex 文档,如下所示:
PyMuPDF4LLM supports direct conversion to a LLamaIndex document. A document is first converted into Markdown format and then a LlamaIndex document is returned as follows:
import pymupdf4llm
llama_reader = pymupdf4llm.LlamaMarkdownReader()
llama_docs = llama_reader.load_data("input.pdf")
和 PyMuPDF Pro 一起使用#
Using with PyMuPDF Pro
对于 Office 文档支持, PyMuPDF4LLM 可与 PyMuPDF Pro 无缝协作。假设您已安装 PyMuPDF Pro,您将能够按预期使用 Office 文档:
import pymupdf4llm
import pymupdf.pro
pymupdf.pro.unlock()
md_text = pymupdf4llm.to_markdown("sample.doc")
如您所见, PyMuPDF Pro 功能将在 PyMuPDF4LLM 上下文中可用!
For Office document support, PyMuPDF4LLM works seamlessly with PyMuPDF Pro. Assuming you have PyMuPDF Pro installed you will be able to work with Office documents as expected:
import pymupdf4llm
import pymupdf.pro
pymupdf.pro.unlock()
md_text = pymupdf4llm.to_markdown("sample.doc")
As you can see PyMuPDF Pro functionality will be available within the PyMuPDF4LLM context!
API#
API
See the PyMuPDF4LLM API.
更多资源#
Further Resources
示例代码#
Sample code
博客#
Blogs