码迷,mamicode.com
首页 > 编程语言 > 详细

Python 将pdf转换成txt(不处理图片)

时间:2014-07-14 10:10:14      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   文件   

  上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要。查找了很多资料,在linux下要将doc转换成txt确实有难度,所以考虑先将pdf转换成txt。

  师兄推荐使用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中的小工具pdf2txt.py,便能将pdf转换成txt,而且仍保留pdf中的格式,超赞!

  阅读pdf2txt.py的源码,我们可以看到具体的实现步骤,为了以后能处理大规模的pdf文件,这里我们只提取出pdf转换成txt的部分,具体实现代码如下:

# -*- coding: utf-8 -*-  
#----------------------------------------------------- 
#   功能:将pdf转换成txt(不处理图片)
#   作者:chenbjin 
#   日期:2014-07-11
#   语言:Python 2.7.6  
#   环境:linux(ubuntu)
#        PDFMiner20140328(Must be installed)
#   使用:python pdf2txt.py file.pdf
#-----------------------------------------------------

import sys
from pdfminer.pdfinterp import PDFResourceManager,PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
#main
def main(argv) :
    #输出文件名,这里只处理单文档,所以只用了argv[1]
    outfile = argv[1] + .txt
    args = [argv[1]]

    debug = 0
    pagenos = set()
    password = ‘‘
    maxpages = 0
    rotation = 0
    codec = utf-8#输出编码
    caching = True
    imagewriter = None
    laparams = LAParams()
    #
    PDFResourceManager.debug = debug
    PDFPageInterpreter.debug = debug

    rsrcmgr = PDFResourceManager(caching=caching)
    outfp = file(outfile,w)
   #pdf转换 device
= TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, imagewriter=imagewriter)
  
for fname in args: fp = file(fname,rb) interpreter = PDFPageInterpreter(rsrcmgr, device)
     #处理文档对象中每一页的内容
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=True) : page.rotate = (page.rotate+rotation) % 360 interpreter.process_page(page) fp.close() device.close() outfp.close() return if __name__ == __main__ : main(sys.argv)

  下一步将尝试将pdf中的图片进行转换,可以通过http://denis.papathanasiou.org/2010/08/04/extracting-text-images-from-pdf-files/ 进行了解。

参考资料:

1.PDFMiner:http://www.unixuser.org/~euske/python/pdfminer/

Python 将pdf转换成txt(不处理图片),布布扣,bubuko.com

Python 将pdf转换成txt(不处理图片)

标签:style   blog   http   color   使用   文件   

原文地址:http://www.cnblogs.com/chenbjin/p/3837453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!