溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pdf用python讀取的方法

發(fā)布時(shí)間:2020-07-02 14:21:36 來源:億速云 閱讀:198 作者:清晨 欄目:編程語言

這篇文章主要介紹pdf用python讀取的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

python中可以使用pdfminer庫來讀取PDF文件中的內(nèi)容。

安裝命令:

pip install pdfminer
pip install pdfminer3k

python中讀取PDF文件代碼:

from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO
from io import open

def readPDF(pdfFile):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)

    process_pdf(rsrcmgr, device, pdfFile)
    device.close()

    content = retstr.getvalue()
    retstr.close()
    return content

pdfFile = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
outputString = readPDF(pdfFile)
print(outputString)
pdfFile.close()

解析pdf文件用到的類:

PDFParser:從一個(gè)文件中獲取數(shù)據(jù)

PDFDocument:保存獲取的數(shù)據(jù),和PDFParser是相互關(guān)聯(lián)的

PDFPageInterpreter處理頁面內(nèi)容

PDFDevice將其翻譯成你需要的格式

PDFResourceManager用于存儲(chǔ)共享資源,如字體或圖像。

以上是pdf用python讀取的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI