Using PyMuPDF as 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 list of 0-based page numbers.
Installation
$ pip install -U pymupdf4llm
This command will automatically install PyMuPDF if required.
Then in your script do:
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=[...] can be used to provide a list of zero-based page numbers to consider.
Feature Overview:
-
Support for pages with multiple text columns.
-
Support for image and vector graphics extraction:
- Specify
pymupdf4llm.to_markdown("input.pdf", write_images=True). Default isFalse. - Each image or vector graphic on the page will be extracted and stored as an image named
"input.pdf-pno-index.extension"in a folder of your choice. 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). - Any text contained in the images or graphics will be extracted and also become visible as part of the generated image. This behavior can be changed via
force_text=False(text only apears as part of the image).
- Specify
-
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 the 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 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.
- 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 the value of
Metadata
Release files for pymupdf4llm-kuberlab 0.0.28
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pymupdf4llm_kuberlab-0.0.28-py3-none-any.whl | Python 3 | none | any | Details for pymupdf4llm_kuberlab-0.0.28-py3-none-any.whl |
Release files / pymupdf4llm_kuberlab-0.0.28-py3-none-any.whl
| Download URL | pymupdf4llm_kuberlab-0.0.28-py3-none-any.whl |
|---|---|
| Size | 42.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a49973250a85fa455bb546d207d7052048b4afbb9531a141cd8396969c553211
|
|
BLAKE2b-256 checksum How to use checksums |
044fdce0ef7b77dcbefceb0d856523a09b70c74e99989fbac67dfa1ff75039bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Security for pymupdf4llm-kuberlab 0.0.28
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.