How to extract text from a PDF file?

前端 未结 24 2290
孤城傲影
孤城傲影 2020-11-22 14:05

I\'m trying to extract the text included in this PDF file using Python.

I\'m using the PyPDF2 module, and have the following script:

imp         


        
24条回答
  •  借酒劲吻你
    2020-11-22 14:41

    The below code is a solution to the question in Python 3. Before running the code, make sure you have installed the PyPDF2 library in your environment. If not installed, open the command prompt and run the following command:

    pip3 install PyPDF2
    

    Solution Code:

    import PyPDF2
    pdfFileObject = open('sample.pdf', 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObject)
    count = pdfReader.numPages
    for i in range(count):
        page = pdfReader.getPage(i)
        print(page.extractText())
    

提交回复
热议问题