Read pdf page by page
<p>I searched for my question and did not get my answer in the two available questions</p>

<ol>
<li><p><a href="https://stackoverflow.com/questions/12605170/extract-text-per-page-with-python-pdfminer">Extract text per page with Python pdfMiner?</a></p></li>
<li><p><a href="https://stackoverflow.com/questions/21113773/pdfminer-iterating-through-pages-and-converting-them-to-text">PDFMiner - Iterating through pages and converting them to text</a></p></li>
</ol>

<p>Basically I want to iterate over each page because I want to select only that page which has a <strong>certain text</strong>.</p>

<p>I have used <code>pyPdf</code>. It works for almost i can say 90% of the <code>pdfs</code> but sometimes it does not extract the information from a page.</p>

<p>I have used the below code:</p>

<pre><code>import pyPdf
extract = ""        
pdf = pyPdf.PdfFileReader(open('filename.pdf', "rb"))
num_of_pages = pdf.getNumPages()
for p in range(num_of_pages):
  ex = pdf.getPage(6)
  ex = ex.extractText()
  if re.search(r"to be held (at|on)",ex.lower()):
    print 'yes'
    print  ex ,"\n"
    extract = extract + ex + "\n" 
    continue
</code></pre>

<p>The above code works but sometimes some pages don't get extracted.</p>

<p>I also tried using <code>pdfminer</code>, but i could not find how to iterate the pdf in it page by page. <code>pdfminer</code> returns the entire text of the pdf.</p>

<p>I used the below code:</p>

<pre><code>def convert_pdf_to_txt(path):
  rsrcmgr = PDFResourceManager()
  retstr = StringIO()
  codec = 'utf-8'
  laparams = LAParams()
  device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
  fp = file(path, 'rb')
  interpreter = PDFPageInterpreter(rsrcmgr, device)
  password = ""
  maxpages = 0
  caching = True
  pagenos=set()

 for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
    interpreter.process_page(page)

    text = retstr.getvalue()

   fp.close()
   device.close()
   retstr.close()
   return text
</code></pre>

<p>In the above code the text from the pdf comes from the <code>for</code> loop</p>

<pre><code>for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
    interpreter.process_page(page)

    text = retstr.getvalue()
</code></pre>

<p>In this how can I iterated on one page at a time.</p>

<p>The documentation on <code>pdfminer</code> is not understandable. Also there are many versions of the same.</p>

<p>So are there any other packages available for my question or can <code>pdfminer</code> be used for it?</p>