PyPDFium2Parser#
- class langchain_community.document_loaders.parsers.pdf.PyPDFium2Parser(
- extract_images: bool = False,
- *,
- password: str | None = None,
- mode: Literal['single', 'page'] = 'page',
- pages_delimiter: str = '\n\x0c',
- images_parser: BaseImageBlobParser | None = None,
- images_inner_format: Literal['text', 'markdown-img', 'html-img'] = 'text',
Parse a blob from a PDF using PyPDFium2 library.
This class provides methods to parse a blob from a PDF document, supporting various configurations such as handling password-protected PDFs, extracting images, and defining extraction mode. It integrates the ‘PyPDFium2’ library for PDF processing and offers synchronous blob parsing.
- Examples:
Setup:
pip install -U langchain-community pypdfium2
Load a blob from a PDF file:
from langchain_core.documents.base import Blob blob = Blob.from_path("./example_data/layout-parser-paper.pdf")
Instantiate the parser:
from langchain_community.document_loaders.parsers import PyPDFium2Parser parser = PyPDFium2Parser( # password=None, mode="page", pages_delimiter="
- “,
# extract_images = True, # images_to_text = convert_images_to_text_with_tesseract(),
)
Lazily parse the blob:
docs = [] docs_lazy = parser.lazy_parse(blob) for doc in docs_lazy: docs.append(doc) print(docs[0].page_content[:100]) print(docs[0].metadata)
Initialize a parser based on PyPDFium2.
- Parameters:
password (Optional[str]) – 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 (Optional[BaseImageBlobParser]) – 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=”#”/>)
extraction_mode – “plain” for legacy functionality, “layout” for experimental layout mode functionality
extraction_kwargs – Optional additional parameters for the extraction process.
- Returns:
This method does not directly return data. Use the parse or lazy_parse methods to retrieve parsed documents with content and metadata.
- Raises:
ValueError – If the mode is not “single” or “page”.
Methods
__init__
([extract_images, password, mode, ...])Initialize a parser based on PyPDFium2.
lazy_parse
(blob)Lazily parse the blob.
parse
(blob)Eagerly parse the blob into a document or documents.
- __init__(
- extract_images: bool = False,
- *,
- password: str | None = None,
- mode: Literal['single', 'page'] = 'page',
- pages_delimiter: str = '\n\x0c',
- images_parser: BaseImageBlobParser | None = None,
- images_inner_format: Literal['text', 'markdown-img', 'html-img'] = 'text',
Initialize a parser based on PyPDFium2.
- Parameters:
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=”#”/>)
extraction_mode – “plain” for legacy functionality, “layout” for experimental layout mode functionality
extraction_kwargs – Optional additional parameters for the extraction process.
- Returns:
This method does not directly return data. Use the parse or lazy_parse methods to retrieve parsed documents with content and metadata.
- Raises:
ValueError – If the mode is not “single” or “page”.
- Return type:
None
- lazy_parse(
- blob: Blob,
Lazily parse the blob. Insert image, if possible, between two paragraphs. In this way, a paragraph can be continued on the next page.