PyMuPDF Utilities for LLM/RAG
Project description
Using PyMuPDF as a Data Feeder in LLM / RAG Applications
This package converts the pages of a PDF 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 sequence of 0-based page numbers.
PyMuPDF-Layout is an optional extension of PyMuPDF. It offers AI-based improved page layout analysis, for instance entailing a much higher table recognition.
Since version 0.2.0, pymupdf4llm fully supports pymupdf-layout. As part of this, output as plain text or a JSON string is also possible. In addition, every page is automatically OCR'd (based on a number of criteria) provided package opencv-python is installed and Tesseract is available on the platform.
Layout mode is activated with a simple modification of the import statements - for details, please see below.
Installation
$ pip install -U pymupdf4llm
This command will automatically install or upgrade PyMuPDF as required.
To install all Python packages for full support of the layout feature and automatic OCR, you can use the following command version:
$ pip install -U pymupdf4llm[ocr,layout]
This will install opencv-python and pymupdf-layout in addition to pymupdf4llm and pymupdf.
Execution
Legacy Mode
For standard (legacy) markdown extraction, use the following simple script
import pymupdf4llm
md_text = pymupdf4llm.to_markdown("input.pdf")
# now work with the markdown text, e.g. store as a UTF8-encoded file
import pathlib
pathlib.Path("output.md").write_bytes(md_text.encode())
Instead of the filename string as above, one can also provide a PyMuPDF Document.
By default, all pages in the PDF will be processed. If desired, the parameter pages=<sequence> can be used to provide a sequence of zero-based page numbers to consider.
Layout Mode
To activate layout mode, use the following
import pymupdf.layout # activate PyMuPDF-Layout in pymupdf
import pymupdf4llm
# The remainder of the script is unchanged
md_text = pymupdf4llm.to_markdown("input.pdf")
# now work with the markdown text, e.g. store as a UTF8-encoded file
import pathlib
pathlib.Path("output.md").write_bytes(md_text.encode())
Here are the JSON and plain text output versions.
JSON
import pymupdf.layout # activate PyMuPDF-Layout in pymupdf
import pymupdf4llm
json_text = pymupdf4llm.to_json("input.pdf")
# now work with the markdown text, e.g. store as a UTF8-encoded file
import pathlib
pathlib.Path("output.json").write_text(json_text)
Plain Text
import pymupdf.layout # activate PyMuPDF-Layout in pymupdf
import pymupdf4llm
plain_text = pymupdf4llm.to_text("input.pdf")
# now work with the markdown text, e.g. store as a UTF8-encoded file
import pathlib
pathlib.Path("output.txt").write_bytes(plain_text.encode())
Feature Overview:
-
Support for pages with multiple text columns.
-
Support for image and vector graphics extraction:
-
Specify either
write_images=Trueorembed_images=True. Default isFalse. -
Images and vector graphics on the page will be stored as images named
"input.pdf-pno-index.extension"in a folder of your choice or be embedded in the markdown text as base64-encoded strings. The imageextensioncan be chosen to represent a PyMuPDF-supported image format (for instance "png" or "jpg"),pnois the 0-based page number andindexis some sequence number. -
The image files will have width and height equal to the values on the page. The desired resolution can be chosen via parameter
dpi(default:dpi=150). So this is not an actual extraction but rather rendering of the respective page area. -
Any standard text written in image areas will become a visible part of the generated image and otherwise be ignored. This behavior can be changed via
force_text=Truewhich causes the text to also become part of the output.
-
-
Support for page chunks: Instead of returning one large string for the whole document, a list of dictionaries can be generated: one for each page. Specify
data = pymupdf4llm.to_markdown("input.pdf", page_chunks=True). Then, for instance the first item,data[0]will contain a dictionary for the first page with its text and some metadata. -
As a first example for directly supporting LLM / RAG consumers, this version can output LlamaIndex documents:
import pymupdf4llm md_read = pymupdf4llm.LlamaMarkdownReader() data = md_read.load_data("input.pdf") # The result 'data' is of type List[LlamaIndexDocument] # Every list item contains metadata and the markdown text of 1 page.
-
A LlamaIndex document essentially corresponds to Python dictionary, where the markdown text of the page is one of the dictionary values. For instance the text of the first page is the value of
data[0].to_dict().["text"]. -
For details, please consult LlamaIndex documentation.
-
Upon creation of the
LlamaMarkdownReaderall necessary LlamaIndex-related imports are executed. Required related package installations must have been done independently and will not be checked during pymupdf4llm installation.
-
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymupdf4llm-0.2.7.tar.gz.
File metadata
- Download URL: pymupdf4llm-0.2.7.tar.gz
- Upload date:
- Size: 65.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
800f93f85e0e841d393a74c263f2cc2d4f77dc6e6fb2e3d12eaf1843adcadcc2
|
|
| MD5 |
fc3cb6e3b6df3c9a2ff78cf89d0dcf9e
|
|
| BLAKE2b-256 |
29881c8bef03fec9d5e8e47219274d0f977017046e5accf0e8a575dcc42bd1c8
|
File details
Details for the file pymupdf4llm-0.2.7-py3-none-any.whl.
File metadata
- Download URL: pymupdf4llm-0.2.7-py3-none-any.whl
- Upload date:
- Size: 66.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ac6b0344c8bade2c97c3d7ea5eb354c71383a8d1ca177fafc3519dd564273b7
|
|
| MD5 |
309e9bfbe5e9f2ef8a7707e756188ccc
|
|
| BLAKE2b-256 |
c01e5fae75a5dc478e376ab95253c2f611665a4d9e2249667387a975e00fbbcb
|