Skip to main content

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 pdf4llm

This command will automatically install PyMuPDF if required.

Then in your script do:

import pdf4llm

md_text = pdf4llm.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.8:

  • Support for pages with multiple text columns.

  • Support for image and vector graphics extraction:

    1. Specify pdf4llm.to_markdown("input.pdf", write_images=True). Default is False.
    2. 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". Where pno is the 0-based page number and index is some sequence number.
    3. The image files will have width and height equal to the values on the page.
    4. Any text contained in the images or graphics will not be extracted, but become visible as image parts.
  • 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 = pdf4llm.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 pdf4llm
    
    md_read = pdf4llm.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 pdf4llm installation.

Metadata

Release files for pdf4llm 1.28.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdf4llm 1.28.2
File Size Uploaded
pdf4llm-1.28.2.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pdf4llm 1.28.2
File Interpreter ABI Platform
pdf4llm-1.28.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.9 kB

Release files / pdf4llm-1.28.2.tar.gz

Download URL pdf4llm-1.28.2.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fc81f484effdfd4f55ad57146a8ef8332fb6f0c03e3209095427fb28063d11ca
BLAKE2b-256 checksum
How to use checksums
1bb132107ce8d116711b8ba513670265e8c9516aa01be81a64ccf8f271e20941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / pdf4llm-1.28.2-py3-none-any.whl

Download URL pdf4llm-1.28.2-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7b6b6bd716e2aaa2b1f604f838810848ca7eed444496ce141b3c11b3c9e0f4f
BLAKE2b-256 checksum
How to use checksums
122533a8aed65aa1b8fbd819eaf1d6b206bd53e5bdfcc10957ad888d13fb2022
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.28.2 This release

2 release files

1.28.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.0.27

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.22

2 release files

0.0.19

2 release files

0.0.18

1 release file

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page