Skip to main content

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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf4llm-0.3.4.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf4llm-0.3.4-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pdf4llm-0.3.4.tar.gz.

File metadata

  • Download URL: pdf4llm-0.3.4.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for pdf4llm-0.3.4.tar.gz
Algorithm Hash digest
SHA256 288ae6c1599d9fe8ce29d84fbd37c241ac05c640a5aa110a1a826e4f432180c4
MD5 5047e7626ec81faf12b57d84432e3822
BLAKE2b-256 c794f106b299958efa990cd95ac331e705495542c145a9b22901cce1fa343ce0

See more details on using hashes here.

File details

Details for the file pdf4llm-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: pdf4llm-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for pdf4llm-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6706159a3d05212382ea31fde64bc32abe8cd9bd78d6c1cf6e7f1b7144501a16
MD5 cb991fdd074e10a3873b61f2c3162508
BLAKE2b-256 de5cc9c8daf3e5882a51f9946d9e80186520456d646ec155c0809751bdf9973e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page