Everything is working fine except timing.it takes lot time for my file containing 1000 pages and having 100 pages of interest.import refrom PyPDF2 import PdfFileReader, PdfFileWriterimport glob, os# find pagesdef  findText(f, slist):    file = open(f, 'rb')    pdfDoc = PdfFileReader(file)    pages = []    for i in range(pdfDoc.getNumPages()):        content = pdfDoc.getPage(i).extractText().lower()        for s in slist:            if re.search(s.lower(), content) is not None:                if i not in pages:                    pages.append(i)    return pages#extract pagesdef extractPage(f, fOut, pages):    file = open(f, 'rb')    output = PdfFileWriter()    pdfOne = PdfFileReader(file)    for i in pages:        output.addPage(pdfOne.getPage(i))    outputStream = open(fOut, wb)    output.write(outputStream)    outputStream.close()    returnos.chdir(rpath\to\mydir)for pdfFile in glob.glob(*.pdf):    print(pdfFile)    outPdfFile = pdfFile.replace(.pdf,_searched_extracted.pdf)    stringList = [string1, string2]    extractPage(pdfFile, outPdfFile, findText(pdfFile, stringList))Updated code after suggestions is at:https://gist.github.com/pra007/099f10b07be5b7126a36438c67ad7a1f
Extract Pages from PDF based on search in python
python;performance;python 3.x;pdf