7

Which python packages can I use to find out out on which page a specific “search string” is located ?

I looked into several python pdf packages but couldn't figure out which one I should use. PyPDF does not seem to have this functionality and PDFMiner seems to be an overkill for such simple task. Any advice ?

More precise: I have several PDF documents and I would like to extract pages which are between a string “Begin” and a string “End” .

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
user1043144
  • 2,680
  • 5
  • 29
  • 45

5 Answers5

18

I finally figured out that pyPDF can help. I am posting it in case it can help somebody else.

(1) a function to locate the string

def fnPDF_FindText(xFile, xString):
    # xfile : the PDF file in which to look
    # xString : the string to look for
    import pyPdf, re
    PageFound = -1
    pdfDoc = pyPdf.PdfFileReader(file(xFile, "rb"))
    for i in range(0, pdfDoc.getNumPages()):
        content = ""
        content += pdfDoc.getPage(i).extractText() + "\n"
        content1 = content.encode('ascii', 'ignore').lower()
        ResSearch = re.search(xString, content1)
        if ResSearch is not None:
           PageFound = i
           break
     return PageFound

(2) a function to extract the pages of interest

  def fnPDF_ExtractPages(xFileNameOriginal, xFileNameOutput, xPageStart, xPageEnd):
      from pyPdf import PdfFileReader, PdfFileWriter
      output = PdfFileWriter()
      pdfOne = PdfFileReader(file(xFileNameOriginal, "rb"))
      for i in range(xPageStart, xPageEnd):
          output.addPage(pdfOne.getPage(i))
          outputStream = file(xFileNameOutput, "wb")
          output.write(outputStream)
          outputStream.close()

I hope this will be helpful to somebody else

user1043144
  • 2,680
  • 5
  • 29
  • 45
  • Thanks, this was helpful! – Scott B Apr 04 '16 at 22:39
  • Hello Experts, I know it's been a long time but, how could I modify this code to Extract PDF Pages Containing a certain string and creating a new document of them? – suchislife Dec 31 '17 at 14:09
  • I get this error when I try to search a string using the above code and using PyPDF2 and open instead of file in python 3.6. Error: TypeError: cannot use a string pattern on a bytes-like object – Aakash Basu Jun 05 '19 at 03:21
  • 1
    I had to add one more line to convert byte to string explicitly and it worked. content2 = content1.decode("utf-8") – Aakash Basu Jun 05 '19 at 03:32
4

I was able to successfully get the output using the code below.

Code:

import PyPDF2
import re

# Open the pdf file
object = PyPDF2.PdfFileReader(r"C:\TEST.pdf")

# Get number of pages
NumPages = object.getNumPages()

# Enter code here
String = "Enter_the_text_to_Search_here"

# Extract text and do the search
for i in range(0, NumPages):
    PageObj = object.getPage(i)
    Text = PageObj.extractText()
    if re.search(String,Text):
         print("Pattern Found on Page: " + str(i))

Sample Output:

Pattern Found on Page: 7
adiga
  • 34,372
  • 9
  • 61
  • 83
  • Most everything in the code was deprecated with PyPDF2 3.0.0. and I don't know how to update it to the current PyPDF2 version. Please help! – Echeban Feb 04 '23 at 21:37
3

Finding on which page a search string is located in a pdf document using python

PyPDF2

 # import packages
    import PyPDF2
    import re
    
    # open the pdf file
    object = PyPDF2.PdfFileReader(r"source_file_path")
    
    # get number of pages
    NumPages = object.getNumPages()
    
    # define keyterms
    String = "P4F-21B"
    
    # extract text and do the search
    for i in range(0, NumPages):
        PageObj = object.getPage(i)
        Text = PageObj.extractText()
        ResSearch = re.search(String, Text)
        if ResSearch != None:
            print(ResSearch)
            print("Page Number" + str(i+1))

Output:

<re.Match object; span=(57, 64), match='P4F-21B'>
Page Number1

PyMuPDF

import fitz
import re

# load document
doc = fitz.open(r"C:\Users\shraddha.shetty\Desktop\OCR-pages-deleted.pdf")

# define keyterms
String = "P4F-21B"

# get text, search for string and print count on page.
for page in doc:
    text = ''
    text += page.get_text()
    if len(re.findall(String, text)) > 0:
        print(f'count on page {page.number + 1} is: {len(re.findall(String, text))}')
dataninsight
  • 1,069
  • 6
  • 13
  • Near answer duplicate of [this](https://stackoverflow.com/a/70093748/1729265) and [this](https://stackoverflow.com/a/70093539/1729265). – mkl Nov 24 '21 at 14:46
2

In addition to what @user1043144 mentioned,

To use with python 3.x

Use PyPDF2

import PyPDF2

Use open instead of file

PdfFileReader(open(xFile, 'rb'))
SuperNova
  • 25,512
  • 7
  • 93
  • 64
  • I get this error when I try to search a string using the above code and using PyPDF2 and open instead of file in python 3.6. Error: TypeError: cannot use a string pattern on a bytes-like object – Aakash Basu Jun 05 '19 at 03:21
  • I had to add one more line to convert byte to string explicitly and it worked. content2 = content1.decode("utf-8") – Aakash Basu Jun 05 '19 at 03:32
0

updated answer with PYDF2

import re 
import PyPDF2


def pdf_find_text(xfile_pdf, xsearch_string, ignore_case = False):
    '''
    find page(s) on which a given text is located in a pdf
    input: pdf file and the string to search 
    (string to search can be in a regex like 'references\n')
    
    N.B: 
    results need to be checked 
    in case of pdf whose page numbers are not zero indexed , 
    the results seems off (by one page) 
    '''
    
    xlst_res = []
    
    xreader = PyPDF2.PdfFileReader(xfile_pdf)

    for xpage_nr, xpage in enumerate(xreader.pages):
        xpage_text = xpage.extractText()
        xhits = None
        if ignore_case == False:
            xhits = re.search(xsearch_string, xpage_text.lower())
        else:
            xhits = re.search(xsearch_string, xpage_text.lower(), re.IGNORECASE)

        if xhits:
            xlst_res.append(xpage_nr)
    return {'num_pages': xreader.numPages, 'page_hits': xlst_res}

def pdf_extract_pages(xpdf_original, xpdf_new , xpage_start, xpage_end):
    '''
    given a pdf extract a page range and save it in a new pdf file 
    
    '''
    with open(xpdf_original, 'rb') as xfile_1, open(xpdf_new , 'wb') as xfile_2:
        xreader = PyPDF2.PdfFileReader(xfile_1)
        xwriter  = PyPDF2.PdfFileWriter()
        for xpage_nr in range(xpage_start, xpage_end ):
            xwriter.addPage(xreader.getPage(xpage_nr))
        xwriter.write(xfile_2)

user1043144
  • 2,680
  • 5
  • 29
  • 45