文本處理 - 求教使用python庫提取pdf的方法?
問題描述
使用過pypdf 對英文pdf文檔處理比較簡單,但是對中文的支持好像不太好
使用過textract 看文檔支持的格式比較多方法也比較簡單,但是老師出錯
-- coding: utf-8 --import textractimport pyPdfimport pdf2textimport pdfminerimport chardet
text = textract.process('F:ll.pdf',method = ’pdfminer’)print text
這個 出錯是編碼問題-- coding: utf-8 --import textractimport pyPdfimport pdfminerimport chardet
text = textract.process('F:ll.pdf',method = ’pdfminer’)print text
這個出錯類型不清楚少使用了pdf2text庫,但是出錯情況好像不一樣。
pdfminer庫還沒看過,看著好像麻煩一些, 求解一下解析提取中文的pdf的方法。謝謝
問題解答
回答1:之前用過的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’’’
相關文章:
1. javascript - vue 移動端的input 數字輸入優化2. 為什么我ping不通我的docker容器呢???3. javascript - 有什么兼容性比較好的辦法來判斷瀏覽器窗口的類型?4. 關于docker下的nginx壓力測試5. HTML5禁止img預覽該怎么解決?6. 服務器端 - 采用nginx做web服務器,C++開發應用程序 出現拒絕連接請求?7. javascript - npm start 運行’webpack-dev-server’報錯 Cannot find module ’webpack’8. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???9. java - 靜態屬性中的賦值和靜態代碼塊中的賦值有什么區別?10. javascript - nidejs環境設置操作一直出現這種問題怎么解決?
