PyMuPDFLoader#

class langchain_community.document_loaders.pdf.PyMuPDFLoader(file_path: str | PurePath, *, password: str | None = None, mode: Literal['single', 'page'] = 'page', pages_delimiter: str = '\n\x0c', extract_images: bool = False, images_parser: BaseImageBlobParser | None = None, images_inner_format: Literal['text', 'markdown-img', 'html-img'] = 'text', extract_tables: Literal['csv', 'markdown', 'html'] | None = None, headers: dict | None = None, extract_tables_settings: dict[str, Any] | None = None, **kwargs: Any)[source]#

Load and parse a PDF file using ‘PyMuPDF’ library.

This class provides methods to load and parse PDF documents, supporting various configurations such as handling password-protected files, extracting tables, extracting images, and defining extraction mode. It integrates the PyMuPDF library for PDF processing and offers both synchronous and asynchronous document loading.

Examples:

Setup:

pip install -U langchain-community pymupdf

Instantiate the loader:

from langchain_community.document_loaders import PyMuPDFLoader

loader = PyMuPDFLoader(
    file_path = "./example_data/layout-parser-paper.pdf",
    # headers = None
    # password = None,
    mode = "single",
    pages_delimiter = "
“,

# extract_images = True, # images_parser = TesseractBlobParser(), # extract_tables = “markdown”, # extract_tables_settings = None,

)

Lazy load documents:

docs = []
docs_lazy = loader.lazy_load()

for doc in docs_lazy:
    docs.append(doc)
print(docs[0].page_content[:100])
print(docs[0].metadata)

Load documents asynchronously:

docs = await loader.aload()
print(docs[0].page_content[:100])
print(docs[0].metadata)

Initialize with a file path.

Parameters:
  • file_path (str | PurePath) – The path to the PDF file to be loaded.

  • headers (dict | None) – Optional headers to use for GET request to download a file from a web path.

  • password (str | None) – Optional password for opening encrypted PDFs.

  • mode (Literal['single', 'page']) – The extraction mode, either “single” for the entire document or “page” for page-wise extraction.

  • pages_delimiter (str) – A string delimiter to separate pages in single-mode extraction.

  • extract_images (bool) – Whether to extract images from the PDF.

  • images_parser (BaseImageBlobParser | None) – Optional image blob parser.

  • images_inner_format (Literal['text', 'markdown-img', 'html-img']) – The format for the parsed output. - “text” = return the content as is - “markdown-img” = wrap the content into an image markdown link, w/ link pointing to (![body)(#)] - “html-img” = wrap the content as the alt text of an tag and link to (<img alt=”{body}” src=”#”/>)

  • extract_tables (Literal['csv', 'markdown', 'html'] | None) – Whether to extract tables in a specific format, such as “csv”, “markdown”, or “html”.

  • extract_tables_settings (dict[str, Any] | None) – Optional dictionary of settings for customizing table extraction.

  • **kwargs (Any) – Additional keyword arguments for customizing text extraction behavior.

Returns:

This method does not directly return data. Use the load, lazy_load, or aload methods to retrieve parsed documents with content and metadata.

Raises:

ValueError – If the mode argument is not one of “single” or “page”.

Attributes

source

Methods

__init__(file_path, *[, password, mode, ...])

Initialize with a file path.

alazy_load()

A lazy loader for Documents.

aload()

Load data into Document objects.

lazy_load()

A lazy loader for Documents.

load(**kwargs)

Load data into Document objects.

load_and_split([text_splitter])

Load Documents and split into chunks.

__init__(file_path: str | PurePath, *, password: str | None = None, mode: Literal['single', 'page'] = 'page', pages_delimiter: str = '\n\x0c', extract_images: bool = False, images_parser: BaseImageBlobParser | None = None, images_inner_format: Literal['text', 'markdown-img', 'html-img'] = 'text', extract_tables: Literal['csv', 'markdown', 'html'] | None = None, headers: dict | None = None, extract_tables_settings: dict[str, Any] | None = None, **kwargs: Any) None[source]#

Initialize with a file path.

Parameters:
  • file_path (str | PurePath) – The path to the PDF file to be loaded.

  • headers (dict | None) – Optional headers to use for GET request to download a file from a web path.

  • password (str | None) – Optional password for opening encrypted PDFs.

  • mode (Literal['single', 'page']) – The extraction mode, either “single” for the entire document or “page” for page-wise extraction.

  • pages_delimiter (str) – A string delimiter to separate pages in single-mode extraction.

  • extract_images (bool) – Whether to extract images from the PDF.

  • images_parser (BaseImageBlobParser | None) – Optional image blob parser.

  • images_inner_format (Literal['text', 'markdown-img', 'html-img']) – The format for the parsed output. - “text” = return the content as is - “markdown-img” = wrap the content into an image markdown link, w/ link pointing to (![body)(#)] - “html-img” = wrap the content as the alt text of an tag and link to (<img alt=”{body}” src=”#”/>)

  • extract_tables (Literal['csv', 'markdown', 'html'] | None) – Whether to extract tables in a specific format, such as “csv”, “markdown”, or “html”.

  • extract_tables_settings (dict[str, Any] | None) – Optional dictionary of settings for customizing table extraction.

  • **kwargs (Any) – Additional keyword arguments for customizing text extraction behavior.

Returns:

This method does not directly return data. Use the load, lazy_load, or aload methods to retrieve parsed documents with content and metadata.

Raises:

ValueError – If the mode argument is not one of “single” or “page”.

Return type:

None

async alazy_load() AsyncIterator[Document]#

A lazy loader for Documents.

Return type:

AsyncIterator[Document]

async aload() list[Document]#

Load data into Document objects.

Return type:

list[Document]

lazy_load() Iterator[Document][source]#

A lazy loader for Documents.

Return type:

Iterator[Document]

load(**kwargs: Any) list[Document][source]#

Load data into Document objects.

Parameters:

kwargs (Any)

Return type:

list[Document]

load_and_split(text_splitter: TextSplitter | None = None) list[Document]#

Load Documents and split into chunks. Chunks are returned as Documents.

Do not override this method. It should be considered to be deprecated!

Parameters:

text_splitter (Optional[TextSplitter]) – TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter.

Returns:

List of Documents.

Return type:

list[Document]

Examples using PyMuPDFLoader