文本處理 - 求教使用python庫(kù)提取pdf的方法?
問(wèn)題描述
使用過(guò)pypdf 對(duì)英文pdf文檔處理比較簡(jiǎn)單,但是對(duì)中文的支持好像不太好
使用過(guò)textract 看文檔支持的格式比較多方法也比較簡(jiǎn)單,但是老師出錯(cuò)
-- coding: utf-8 --import textractimport pyPdfimport pdf2textimport pdfminerimport chardet
text = textract.process('F:ll.pdf',method = ’pdfminer’)print text
這個(gè) 出錯(cuò)是編碼問(wèn)題-- coding: utf-8 --import textractimport pyPdfimport pdfminerimport chardet
text = textract.process('F:ll.pdf',method = ’pdfminer’)print text
這個(gè)出錯(cuò)類(lèi)型不清楚少使用了pdf2text庫(kù),但是出錯(cuò)情況好像不一樣。
pdfminer庫(kù)還沒(méi)看過(guò),看著好像麻煩一些, 求解一下解析提取中文的pdf的方法。謝謝
問(wèn)題解答
回答1:之前用過(guò)的pdfminer pip install pdfminer
# -*- coding: utf-8 -*-from bs4 import BeautifulSoupimport requestsimport refrom pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreterfrom pdfminer.converter import TextConverterfrom pdfminer.layout import LAParamsfrom cStringIO import StringIO#from io import StringIO for python3from io import openfrom pdfminer.pdfpage import PDFPagedef pdf_txt(url): rsrcmgr = PDFResourceManager() retstr = StringIO() codec = ’utf-8’ laparams = LAParams() device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) f = requests.get(url).content fp = StringIO(f) 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) fp.close() device.close() str = retstr.getvalue() retstr.close() return strtxt=tpdf_txt(’http://pythonscraping.com/pages/warandpeace/chapter1.pdf’)print txt#如果pdf含有中文,輸出到文件#open(’pdf.txt’,’wb’).write(txt)python readpdf.py’’’CHAPTER I'Well, Prince, so Genoa and Lucca are now just family estates oftheBuonapartes. But I warn you, if you don’t tell me that thismeans war,if you still try to defend the infamies and horrorsperpetrated bythat Antichrist- I really believe he is Antichrist- I willhavenothing more to do with you and you are no longer my friend,no longermy ’faithful slave,’ as you call yourself! But how do youdo? I seeI have frightened you- sit down and tell me all the news.'It was in July, 1805, and the speaker was the well-knownAnnaPavlovna Scherer, maid of honor and favorite of theEmpress MaryaFedorovna. With these words she greeted PrinceVasili Kuragin, a manof high rank and importance, who was thefirst to arrive at herreception. Anna Pavlovna had had a cough forsome days. She was, asshe said, suffering from la grippe; grippebeing then a new word inSt. Petersburg, used only by the elite.All her invitations without exception, written in French,anddelivered by a scarlet-liveried footman that morning, ran as’’’
相關(guān)文章:
1. java固定鍵值轉(zhuǎn)換,使用枚舉實(shí)現(xiàn)字典?2. vim - win10無(wú)法打開(kāi)markdown編輯器3. mysql - 千萬(wàn)數(shù)據(jù) 分頁(yè),當(dāng)偏移量 原來(lái)越大時(shí),怎么優(yōu)化速度4. 如何解決tp6在zend中無(wú)代碼提示5. javascript - 有沒(méi)有類(lèi)似高鐵管家的時(shí)間選擇插件6. 這是什么情況???7. python - flask學(xué)習(xí),user_syy添加報(bào)role is invalid keyword for User.8. css - BEM 中塊(Block)有木有什么標(biāo)準(zhǔn) 何時(shí)決定一個(gè)部分提取為塊而不是其父級(jí)的元素呢(Element)?~9. css3 - less或者scss 顏色計(jì)算的知識(shí)應(yīng)該怎么學(xué)?或者在哪里學(xué)?10. javascript - 微信網(wǎng)頁(yè)開(kāi)發(fā)從菜單進(jìn)入頁(yè)面后,按返回鍵沒(méi)有關(guān)閉瀏覽器而是刷新當(dāng)前頁(yè)面,求解決?
