[Python] How to extract text from a PDF file via python?

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm extracting this PDF's text using the PyPDF2 Python package (version 1.27.2):

    import PyPDF2

    with open("sample.pdf", "rb") as pdf_file:
    read_pdf = PyPDF2.PdfFileReader(pdf_file)
    number_of_pages = read_pdf.getNumPages()
    page = read_pdf.pages[0]
    page_content = page.extractText()
    print(page_content)


    I get this output which is different from the PDF document:

    ! " # $ % # $ % &% $ &' ( ) * % + , - % . / 0 1 ' * 2 3% 4
    5
    ' % 1 $ # 2 6 % 3/ % 7 / ) ) / 8 % &) / 2 6 % 8 # 3" % 3" * % 31 3/ 9 # &)
    %


    How can I extract the text as is in the PDF document?

    Continue reading...

Compartilhe esta Página