python 读取 pdf 文档

来源:互联网 发布:mysql root用户无权限 编辑:程序博客网 时间:2024/05/17 05:02


这个图片是使用的流程说明,看着是有点绕的,分解来看(学自慕课)


首先使用 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') 
原创粉丝点击