[Python] convert from pdf to text: lines and words are broken

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    I want to convert a pdf file to text by PyPDF2 but converted text looks differents from PDF file. Specifically, one line in PDF is broken into multiple lines in text and words may be broken as well. Attached is the PDF and the text file I got with the code below. Could anyone help me fix this issue?

    from PyPDF2 import PdfFileReader


    def extract_pdf_text(file_path=""):
    reader = PdfFileReader(file_path)

    text = ""

    # Loop over all the pdf pages.
    for page in reader.pages:
    text = text + page.extractText()
    return text

    pdf_text = extract_pdf_text("PDF file path")


    pdf file

    converted text

    Continue reading...

Compartilhe esta Página