Python converts PDF to TXT (does not process pictures)
The previous article has described the simple Python crawl page download document, but the downloaded documents are more doc or PDF, there are still many restrictions on data processing, so converting doc/pdf into TXT is particularly important. Looking for a lot of information, it is difficult to convert doc to txt under Linux, so consider converting the PDF to txt first.
Brother recommended the use of Pdfminer to deal with, tried some, really good results, here and everyone to share.
Pdfminer Introduction: Pdfminer is a tool for extracting information from PDF documents. Unlike other pdf-related tools, it focuses entirely on getting and analyzing text data. Interested students please visit through the official website for detailed review, through the Pdfminer in the gadget PD f2txt.py, you can convert the PDF to TXT, and still retain the format in the PDF, awesome!
Read the pdf2txt.py source code, we can see the specific implementation steps, in order to deal with large-scale PDF files, here we only extract the PDF converted to TXT, the implementation code is as follows:
#-*-Coding:utf-8-*-#-----------------------------------------------------# function: Convert PDF to TXT (do not process picture) # Chenbjin # Day Period: 2014-07-11# language: Python 2.7.6 # Environment: Linux (Ubuntu) # PDFMiner20140328 (must be installed) # using: 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): #输出文件名, only the single document is processed, so only 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 P Dfpageinterpreter.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=pass Word, caching=caching, check_extractable=true): Page.rotate = (page.rotate+rotation) % interpreter.process_page (page) fp.close () Device.close () outfp.close () returnif __name__ = = ' __main__ ': Main (SYS.ARGV)
The next step is to try to convert the pictures in the PDF, which you can learn through http://denis.papathanasiou.org/2010/08/04/extracting-text-images-from-pdf-files/.
Resources:
1.pdfminer:http://www.unixuser.org/~euske/python/pdfminer/
Python converts PDF to TXT (does not process pictures)