Below for you to share an example of using Python to output the PDF as TXT, with a good reference value, I hope to help you. Come and see it together.
A week ago a classmate asked me this, because before the competition in Huawei, so after the game to see, it is said to use the Pdfminer this package. Then the installation process is simple:
sudo pip install pdfminer;
There is no error in the middle. As to how to call, I also did not have a good study of Pdfminer this library, so began the Baidu ...
Official document:http://www.unixuser.org/~euske/python/pdfminer/index.html
Written entirely using Python. (for 2.4 or later versions)
Parse, analyze, and convert to PDF documents.
Support for PDF-1.7 specifications. Almost
Support for CJK Language and vertical scripting scripts.
Support for various font types (Type1, TrueType, Type3, and CID).
Support for basic encryption (RC4).
PDF and HTML conversion.
Outline (TOC) extraction.
Tag content extraction.
Rebuilds the original layout by grouping text blocks.
Some of the basic classes
Pdfparser: Getting data from a file
Pdfdocument: Save acquired data, and Pdfparser are interrelated
Pdfpageinterpreter Processing page Content
Pdfdevice translate it into the format you need
Pdfresourcemanager is used to store shared resources, such as fonts or images.
A simple implementation
Read Test.pdf output to output.txt:
#-*-Coding:utf-8-*-from pdfminer.pdfparser import pdfparser from pdfminer.pdfdocument import pdfdocument from Pdfmin Er.pdfpage Import pdfpage from pdfminer.pdfpage import pdftextextractionnotallowed from pdfminer.pdfinterp Import Pdfresourcemanager from Pdfminer.pdfinterp import pdfpageinterpreter from Pdfminer.pdfdevice import Pdfdevice from Pdfminer.layout Import * from pdfminer.converter import pdfpageaggregator import os fp = open (' test.pdf ', ' RB ') #来创建一个pdf文 File Parser parser = pdfparser (fp) #创建一个PDF文档对象存储文档结构 document = Pdfdocument (parser) # Check if the file allows text extraction if not document.is_extractabl E:raise pdftextextractionnotallowed Else: # Create a PDF Explorer object to store the shared Rewards Resource Rsrcmgr=pdfresourcemanager () # Setting parameters for Analysis Laparams=l Aparams () # Create a PDF device Object # Device=pdfdevice (rsrcmgr) Device=pdfpageaggregator (rsrcmgr,laparams=laparams) # Create a PDF interpreter pair Like Interpreter=pdfpageinterpreter (rsrcmgr,device) # Process each page for page in Pdfpage.create_pages (document): Interpreter.proc Ess_page (page) # accepts the Ltpage object for this page LayoUt=device.get_result () for x in Layout:if (Isinstance (x,lttextboxhorizontal)): With open (' output.txt ', ' a ') as F: F.write (X.get_text (). Encode (' utf-8 ') + ' \ n ')