Skip to content

pdf

PDF tools.

Functions:

extract_text_from_pdf

extract_text_from_pdf(file_path: str) -> str

Extract text from a PDF file using pypdf.

The result is the same as the one returned by PyPDFLoader.

Source code in src/rago/retrieval/tools/pdf.py
32
33
34
35
36
37
38
39
40
41
42
43
44
def extract_text_from_pdf(file_path: str) -> str:
    """
    Extract text from a PDF file using pypdf.

    The result is the same as the one returned by PyPDFLoader.
    """
    reader = PdfReader(file_path)
    pages = []
    for page in reader.pages:
        text = page.extract_text()
        if text:
            pages.append(text)
    return ' '.join(pages)

is_pdf

is_pdf(file_path: str | Path) -> bool

Check if a file is a PDF by reading its header.

Parameters:

  • file_path (str) –

    Path to the file to be checked.

Returns:

  • bool

    True if the file is a PDF, False otherwise.

Source code in src/rago/retrieval/tools/pdf.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def is_pdf(file_path: str | Path) -> bool:
    """
    Check if a file is a PDF by reading its header.

    Parameters
    ----------
    file_path : str
        Path to the file to be checked.

    Returns
    -------
    bool
        True if the file is a PDF, False otherwise.
    """
    try:
        with open(file_path, 'rb') as file:
            header = file.read(4)
            return header == b'%PDF'
    except IOError:
        return False