使用Python3.4提取PDF文本

标签 pdf python-3.x pdf-parsing pdfminer

pdf文件中的文本是文本格式,不是扫描的。 PDFMiner不支持python3,有其他解决方案吗?

最佳答案

还有 pdfminer2 分支,支持 python 3.4,可通过 pip3 获得。 https://github.com/metachris/pdfminer

This thread帮我把一些东西拼凑在一起。

from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO, BytesIO

def readPDF(pdfFile):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)

    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()
    for page in PDFPage.get_pages(pdfFile, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)

    device.close()
    textstr = retstr.getvalue()
    retstr.close()
    return textstr

if __name__ == "__main__":
    #scrape = open("../warandpeace/chapter1.pdf", 'rb') # for local files
    scrape = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf") # for external files
    pdfFile = BytesIO(scrape.read())
    outputString = readPDF(pdfFile)
    print(outputString)
    pdfFile.close()    

关于使用Python3.4提取PDF文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023793/

相关文章:

德尔福5 : Total number of page in pdf

java - 使用 iText 在每个页面中进行数字签名

java - 使用 jasper 报告,复选符号 (✓) 不显示在 PDF 中

python - 如何在Jupyter中使用pandas的 `to_latex`方法获取可以直接在LaTeX中使用的代码?

python - Beautifulsoup 过滤器结果在 "for i"循环中引发 KeyError

python - 从 pdf 中提取表格

PDF 交叉引用流

javascript - 通过 javascript 从浏览器打印 PDF

php - 在 PHP 中创建 PDF 页面

javascript - 为什么我会收到 'CSRF token missing or incorrect' 错误?