2

我正在尝试使用 PDFMiner 从 PDF 文件中提取文本(在使用 Python 中的 PDFMiner 从 PDF 文件中提取文本中找到的代码?)。除了path/to/pdf,我没有更改代码。令人惊讶的是,该代码返回了同一文档的多个副本。我对其他 pdf 文件得到了相同的结果。我需要传递其他参数还是我遗漏了什么?非常感谢任何帮助。以防万一,我提供代码:

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

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = file(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()
    fstr = ''
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages,    password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)

        str = retstr.getvalue()
        fstr += str

    fp.close()
    device.close()
    retstr.close()
    return fstr

print convert_pdf_to_txt("test.pdf")
4

3 回答 3

9

我的回答在您引用的线程中有点不正确。我发现了错误并忘记更新我的答案。

因为 pdfminer 的文档非常稀疏,所以我无法完全解释为什么它会这样工作。希望更了解 pdfminer 库的人能给我们一些见解。

我所知道的是你必须text = retstr.getvalue()在 for 循环之外做。我只能假设retstr正在更新,就好像我们final_text += text在 for 循环中所做的那样,所以一旦完成,我们只需text = retstr.getvalue()要从所有页面中获取文本。

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

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = file(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages,password=password,caching=caching, check_extractable=True):

        interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text

print convert_pdf_to_txt("test.pdf")

希望这有帮助!

于 2014-11-05T20:40:30.113 回答
1

对于 python 3,DuckPuncher 的代码只需要稍作调整:

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

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = io.StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = open(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages,password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text
于 2019-04-27T13:17:55.360 回答
0

在我看来,如果你想使用循环来引入子文档,你必须在每个循环中更新“解释器”、“rsrcmgr”和“设备”。这里是一个例子:https ://stackoverflow.com/a/70355256/8766822

于 2021-12-15T09:40:31.647 回答