Everything is working fine except timing. it takes lot time for my file containing 1000 pages and having 100 pages of interest.
import re
from PyPDF2 import PdfFileReader, PdfFileWriter
import glob, os
# find pages
def 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 pages
def 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()
return
os.chdir(r"path\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