深入学习python解析并读取PDF文件内容的方法

这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应用。主要参考了一些已有的博客内容,代码。
  主要思路是首先利用一个做项目的形式,描述所做的问题,运行环境,和需要安装的库,然后写代码,此代码是在python2.7中运行,小编也写出在python3.6中运行的代码,并详细解释python2.7和python3.6中python库的一些不同之处,最后详细的解释了代码的意思,和库的思路,最终的目的就让我们理解,并学会应用python解析并读取PDF文件内容的方法。
一,问题描述
  利用python读取PDF文本内容
二,运行环境
  python 3.6
三, 需要安装的库

pip install pdfminer

对pdfminer的简单介绍,官网介绍如下:
  PDFMiner is a tool for extracting information from PDF documents. Unlike other PDF-related tools, it focuses entirely on getting and analyzing text data. PDFMiner allows to obtain the exact location of texts in a page, as well as other information such as fonts or lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purposes instead of text analysis.
翻译是这样的:
PDFMiner是一个从PDF文档中提取信息的工具。与其他pdf相关的
工具不同,它完全专注于获取和分析文本数据。PDFMiner允许获取
页面中文本的确切位置,以及其他信息,比如字体或行。它包括一
个PDF转换器,可以将PDF文件转换成其他文本格式(如HTML)。
它有一个可扩展的PDF解析器,可以用于其他目的而不是文本分析。
四,实现源代码(其中代码1和代码2都是python2.7实现的)
python2中相关库的安装
  python各种库下载地址:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
https://pypi.python.org/pypi/
找到pdfminer,下载,然后上传到服务器上,直接安装即可。
  但是由于Python2和python3版本之间的不兼容,所以对应不同的版本,我们这里需要使用不同的特定环境。
    其中的Pdfminer版本是pdfminer 20140328(对于python2.x的测试没有任何问题)

 #_*_coding:utf-8_*_
 
from pdfminer.pdfparser import  PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfdevice import PDFDevice
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal,LAParams
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
 
 
def parse(Path,Save_name):
    
    parser = PDFParser(Path)
    document = PDFDocument(parser)
  
 
    if not document.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        rsrcmgr = PDFResourceManager()
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr,laparams=laparams)
        interpreter = PDFPageInterpreter(rsrcmgr,device)
 
        for page in PDFPage.create_pages(document):
            interpreter.process_page
  • 6
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值