Python converts PDF to TXT (does not process pictures)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.