[Python] PyPDF2 - PdfFileReader - cannot extract text

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am looping through a directory and reading in numerous PDFs. I am extracting all text information from each page using a loop.

    5/13 PDFs are throwing an error when trying to use .getNumPages(): Exception has occurred: ValueError invalid literal for int() with base 10: b''. I believe this error is occurring because the object (PyPDF2) is showing numPages: 0.

    Current Code


    dir = os.listdir(directory)

    for f in dir:
    object = PyPDF2.PdfFileReader(directory + '\\' + f)

    NumPages = object.getNumPages()
    text_output = "" # Initiate Variable

    # Loop through all pages and extract/merge text
    with open(directory + '\\' + f, mode='rb') as FileName:
    reader = PyPDF2.PdfFileReader(FileName)
    for p_num in range(0, NumPages):
    page = reader.getPage(p_num)
    text_output = text_output + '\n' + 'PAGE: ' + \
    str(p_num + 1) + '\n' + page.extractText()


    I added an image showing the object data where numPages: 0

    I cannot figure out why only certain PDFs are having this issue. Any help would be appreciated!!

    Continue reading...

Compartilhe esta Página