wordpress为何登,东莞优化网站关键词优化,湖南省郴州市邮编,网站建设基础教案上一篇文章中已经介绍了简单的python爬网页下载文档#xff0c;但下载后的文档多为doc或pdf#xff0c;对于数据处理仍然有很多限制#xff0c;所以将doc#xff0f;pdf转换成txt显得尤为重要。查找了很多资料#xff0c;在linux下要将doc转换成txt确实有难度#xff0c;…上一篇文章中已经介绍了简单的python爬网页下载文档但下载后的文档多为doc或pdf对于数据处理仍然有很多限制所以将docpdf转换成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 sysfrom pdfminer.pdfinterp import PDFResourceManager,PDFPageInterpreterfrom pdfminer.converter import TextConverterfrom pdfminer.layout import LAParamsfrom pdfminer.pdfpage import PDFPage#maindef main(argv) :#输出文件名这里只处理单文档所以只用了argv1outfile argv[1] ‘.txt‘args [argv[1]]debug 0pagenos set()password ‘‘maxpages 0rotation 0codec ‘utf-8‘ #输出编码caching Trueimagewriter Nonelaparams LAParams()#PDFResourceManager.debug debugPDFPageInterpreter.debug debugrsrcmgr PDFResourceManager(cachingcaching)outfp file(outfile,‘w‘)#pdf转换device TextConverter(rsrcmgr, outfp, codeccodec, laparamslaparams,imagewriterimagewriter)for fname in args:fp file(fname,‘rb‘)interpreter PDFPageInterpreter(rsrcmgr, device)#处理文档对象中每一页的内容for page in PDFPage.get_pages(fp, pagenos,maxpagesmaxpages, passwordpassword,cachingcaching, check_extractableTrue) :page.rotate (page.rotaterotation) % 360interpreter.process_page(page)fp.close()device.close()outfp.close()returnif __name__ ‘__main__‘ : main(sys.argv)下一步将尝试将pdf中的图片进行转换可以通过http://denis.papathanasiou.org/2010/08/04/extracting-text-images-from-pdf-files/ 进行了解。参考资料1.PDFMinerhttp://www.unixuser.org/~euske/python/pdfminer/