SoFunction
Updated on 2024-11-19

Python2.7 read PDF files of the method examples

In this paper, an example of Python2.7 to read PDF files. Shared for your reference, as follows:

This article sample code using the Python version is 2.7, you need to download the plug-in is PDFMiner, the download address is /~euske/python/pdfminer/, the address of the installation method, I will not go into detail, it is necessary to explain that Python2 can only use PDFMiner, Python3 can not be used. Python3 can use PDFMiner3K, download address /pypi/pdfminer3k/. The use of the two plug-ins are largely similar, here I take Python2 as an example, the use of PDFMiner plug-ins. Code is as follows:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
from  import PDFParser
from  import PDFDocument
from  import PDFPage
from  import PDFTextExtractionNotAllowed
from  import PDFResourceManager
from  import PDFPageInterpreter
from  import PDFDevice
from  import LAParams
from  import PDFPageAggregator
# Get the document object, you just replace it with your own filename.
fp=open("","rb")
# Create an interpreter associated with a document
parser=PDFParser(fp)
#PDF Document Objects
doc=PDFDocument(parser)
# Linking interpreters and document objects
parser.set_document(doc)
#doc.set_paeser(parser)
# Initialization documentation
#("")
#Create PDF Explorer
resource=PDFResourceManager()
#Parameter Analyzer
laparam=LAParams()
#Creating an aggregator
device=PDFPageAggregator(resource,laparams=laparam)
# Create PDF page interpreter
interpreter=PDFPageInterpreter(resource,device)
# Use the document object to get a collection of pages
for page in PDFPage.create_pages(doc):
  # Use the page interpreter to read
  interpreter.process_page(page)
  #Use aggregators to get content
  layout=device.get_result()
  for out in layout:
    if hasattr(out, "get_text"):
      print out.get_text()

Readers interested in more Python related content can check out this site's topic: theSummary of Python file and directory manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.