从pdf提取文本-PyPDF2

时间:2019-09-10 23:19:14

标签: python-3.x pypdf2

我正在按照页面上的教程从pdf中提取文本:

http://www.blog.pythonlibrary.org/2018/06/07/an-intro-to-pypdf2/

我可以打印pdf信息,但不能打印页面内容。它不会引发任何错误,但是我也看不到pdf文本

可能是什么问题?

from PyPDF2 import PdfFileReader


def get_info(path):
    with open(path, 'rb') as f:
        pdf = PdfFileReader(f)
        info = pdf.getDocumentInfo()
        number_of_pages = pdf.getNumPages()

    #print(info)

    author = info.author
    creator = info.creator
    producer = info.producer
    subject = info.subject
    title = info.title


    print(author)
    print(creator)
    print(producer)
    print(subject)
    print(title)

def text_extractor(path):
    with open(path, 'rb') as f:
        pdf = PdfFileReader(f)

        # get the first page
        page = pdf.getPage(0)
        print(page)
        print('Page type: {}'.format(str(type(page))))

        text = page.extractText()

        print(text) #THIS PART SHOULD PRINT TEXT FROM PDF, BUT DOESNT WORK



if __name__ == '__main__':
        #URL PDF: https://oficinavirtual.ugr.es/apli/solicitudPAU/test.pdf
    path = 'test.pdf'
    get_info(path)
    print("\n"*2)
    text_extractor(path)

1 个答案:

答案 0 :(得分:0)

尽管这不是解决方案,但是您可以使用pip轻松安装pdfminer3并使用最少的可复制示例here