在python 3中将pdf文件读取到文本文件,但它会生成类似AttributeError的错误:' PDFObjRef'对象没有属性' decode'

时间:2018-05-23 08:33:48

标签: python pdfminer pdftotext

使用pdfminer库读取pdf文件以在python3中提取文本(我使用pip install pdfminer.six安装了包)

import io
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage


def pdfparser(data):

    fp = open(data, 'rb')
    rsrcmgr = PDFResourceManager()
    retstr = io.StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    # Create a PDF interpreter object.

    interpreter = PDFPageInterpreter(rsrcmgr, device)
    # Process each page contained in the document.


    for page in PDFPage.get_pages(fp):
        interpreter.process_page(page)
        data =  retstr.getvalue()
    print(data)

0 个答案:

没有答案