python 讀取 pdf 文檔


這個圖片是使用的流程說明,看着是有點繞的,分解來看(學自慕課


首先使用 open 方法或者  urlopen  打開本場文檔或者網絡文檔(一般會這麼做因爲考慮到文檔太大,對網絡服務器負擔也很大)生成文檔對象,以下的方法之中的網絡鏈接已經存在了大哭

  1. # 獲取文檔對象  
  2. pdf0 = open('sampleFORtest.pdf','rb')  
  3. pdf1 = urlopen('http://www.tencent.com/zh-cn/content/ir/an/2016/attachments/20160321.pdf')  
接着創建 文檔解析器 和 PDF文檔對象 並將他們相互關聯

  1. # 創建一個與文檔關聯的解析器  
  2. parser = PDFParser(pdf0)  
  3.   
  4. # 創建一個PDF文檔對象  
  5. doc = PDFDocument()  
  6.   
  7. # 連接兩者  
  8. parser.set_document(doc)  
  9. doc.set_parser(parser)  
 PDF文檔對象 進行初始化,如果文檔本身進行了加密,則需要在加入 password 參數
  1. # 文檔初始化  
  2. doc.initialize('')  
  

創建 PDF資源管理器  參數分析器 

  1. # 創建PDF資源管理器  
  2. resources = PDFResourceManager()  
  3.   
  4. # 創建參數分析器  
  5. laparam = LAParams()  
創建一個 聚合器 ,並接收 PDF資源管理器  參數分析器 作爲參數

  1. # 創建一個聚合器,並接收資源管理器,參數分析器作爲參數  
  2. device = PDFPageAggregator(resources,laparams=laparam)  
最後創建一個 頁面解釋器 ,將 PDF資源管理器  聚合器 作爲參數

  1. # 創建一個頁面解釋器  
  2. interpreter = PDFPageInterpreter(resources,device)  
這樣 頁面解釋器 就具有對PDF文檔進行編碼,解釋成Python能夠識別的格式


最後呢,使用 PDF文檔對象 的 get_pages()方法 從PDF文檔中讀取出頁面集合,接着使用 頁面解釋器    對頁面集合逐一讀取,再調用 聚合器   get_result()方法 將頁面逐一放置到 layout 之中,最後商用 layout  get_text()方法 獲取每一頁的 text 

  1. for page in doc.get_pages():  
  2.     # 使用頁面解釋器讀取頁面  
  3.     interpreter.process_page(page)  
  4.     # 使用聚合器讀取頁面頁面內容  
  5.     layout = device.get_result()  
  6.   
  7.     for out in layout:  
  8.         if hasattr(out,'get_text'):     # 因爲文檔中不只有text文本  
  9.             pprint(out.get_text())  
需要注意的是在PDF文檔中不只有 text 還可能有圖片等等,爲了確保不出錯先判斷對象是否具有 get_text()方法 

完整的代碼

  1. # encoding:utf-8  
  2. '''  
  3. @author:  
  4. @time:  
  5. '''  
  6. from pdfminer.converter import PDFPageAggregator  
  7. from pdfminer.layout import LAParams  
  8. from pdfminer.pdfparser import PDFParser, PDFDocument  
  9. from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter  
  10. from pdfminer.pdfdevice import PDFDevice  
  11. from pprint import pprint  
  12. from urllib.request import urlopen  
  13.   
  14. # 獲取文檔對象  
  15. pdf0 = open('sampleFORtest.pdf','rb')  
  16. pdf1 = urlopen('http://www.tencent.com/zh-cn/content/ir/an/2016/attachments/20160321.pdf')  
  17.   
  18. # 創建一個與文檔關聯的解釋器  
  19. parser = PDFParser(pdf0)  
  20.   
  21. # 創建一個PDF文檔對象  
  22. doc = PDFDocument()  
  23.   
  24. # 連接兩者  
  25. parser.set_document(doc)  
  26. doc.set_parser(parser)  
  27.   
  28. # 文檔初始化  
  29. doc.initialize('')  
  30.   
  31. # 創建PDF資源管理器  
  32. resources = PDFResourceManager()  
  33.   
  34. # 創建參數分析器  
  35. laparam = LAParams()  
  36.   
  37. # 創建一個聚合器,並接收資源管理器,參數分析器作爲參數  
  38. device = PDFPageAggregator(resources,laparams=laparam)  
  39.   
  40. # 創建一個頁面解釋器  
  41. interpreter = PDFPageInterpreter(resources,device)  
  42.   
  43. # 使用文檔對象獲取頁面的集合  
  44. for page in doc.get_pages():  
  45.     # 使用頁面解釋器讀取頁面  
  46.     interpreter.process_page(page)  
  47.     # 使用聚合器讀取頁面頁面內容  
  48.     layout = device.get_result()  
  49.   
  50.     for out in layout:  
  51.         if hasattr(out,'get_text'):     # 因爲文檔中不只有text文本  
  52.             pprint(out.get_text())  
素材選取是官方提供的



運行的結果:

  1. 'Preemptive Information Extraction using Unrestricted Relation Discovery\n'  
  2. 'Yusuke Shinyama\n'  
  3. 'Satoshi Sekine\n'  
  4. 'New York University\n715, Broadway, 7th Floor\nNew York, NY, 10003\n'  
  5. '{yusuke,sekine}@cs.nyu.edu\n'  
  6. 'Abstract\n'  
  7. ('We are trying to extend the boundary of\n'  
  8.  'Information Extraction (IE) systems. Ex-\n'  
  9.  'isting IE systems require a lot of time and\n'  
  10.  'human effort to tune for a new scenario.\n'  
  11.  'Preemptive Information Extraction is an\n'  
  12.  'attempt to automatically create all feasible\n'  
  13.  'IE systems in advance without human in-\n'  
  14.  'tervention. We propose a technique called\n'  
  15.  'Unrestricted Relation Discovery that dis-\n'  
  16.  'covers all possible relations from texts and\n'  
  17.  'presents them as tables. We present a pre-\n'  
  18.  'liminary system that obtains reasonably\n'  
  19.  'good results.\n') 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章