PyMuPDF Utilities for LLM/RAG
Project description
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.
New features as of v0.0.2:
-
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 a PNG image named
"input.pdf-pno-index.png"
in the folder of"input.pdf"
. Wherepno
is the 0-based page number andindex
is some sequence number. - The image files will have width and height equal to the values on the page.
- Any text contained in the images or graphics will not be extracted, but become visible as image parts.
- 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 = 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
LlamaMarkdownReader
all 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
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
File details
Details for the file pymupdf4llm-0.0.10.tar.gz
.
File metadata
- Download URL: pymupdf4llm-0.0.10.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a1f323fb939a350b6e4916ec0f205295c9690458d991dbcda6b5ad1a2af4064 |
|
MD5 | 19ddf1e0cf2c16739313e9adcc7f9a7c |
|
BLAKE2b-256 | a1144fcf08caebd7841ecad03e24b95f632e13557640d67aa470104272d0a643 |
File details
Details for the file pymupdf4llm-0.0.10-py3-none-any.whl
.
File metadata
- Download URL: pymupdf4llm-0.0.10-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3524b8f67cb5662e9dfa44432b8ffa92aa8254816728779238fa2d1dbe909b73 |
|
MD5 | 5034257a66df6673179ee621f3b7f15c |
|
BLAKE2b-256 | f7219d31f47dbb8646b89f934a58a2f95a51671d691a7a46f3c9e71a3b2bf874 |