#๐Ÿ”’ Keyword Scanner Program for PDF

37 messages ยท Page 1 of 1 (latest)

open maple
#

I am trying to print out the sentence for where the word was found but it just prints out that it founded the word on that page number.

import os
from pdfreader import SimplePDFViewer, PageDoesNotExist

def search_in_file(fname, search_words):
    fd = open(fname, "rb")
    viewer = SimplePDFViewer(fd)
    try:
        while True:
            viewer.render()
            text = "".join(viewer.canvas.strings)
            for word in search_words:
                if word in text:
                    print("The word '{}' was found in '{}' on page {}".format(word, fname, viewer.current_page_number))
            viewer.next()
    except PageDoesNotExist:
        pass

# define keywords
search_words = ['python', 'aws', 'sql']

# define directory
directory = "./"

# Loop through all PDFs in specified directory:
for fname in os.listdir(directory):
    if fname.endswith(".pdf"):
        search_in_file(fname, search_words)
fresh belfryBOT
#

@open maple

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shrewd ermine
#

can you show some output?

#

You're saying it's not printing word in your print statement?

open maple
#

he word 'python' was found in '60-mercurymagazines19-w_mmed11-rPiMvnTCyRuL8g_3GQYjCA.pdf' on page 20
The word 'aws' was found in '60-mercurymagazines19-w_mmed11-rPiMvnTCyRuL8g_3GQYjCA.pdf' on page 28
The word 'python' was found in '60-mercurymagazines19-w_mmed11-rPiMvnTCyRuL8g_3GQYjCA.pdf' on page 55
The word 'aws' was found in '60-mercurymagazines19-w_mmed11-rPiMvnTCyRuL8g_3GQYjCA.pdf' on page 71

#

I need for it to print out the entire sentence where it was founded to be

#

Than I do get this error

#

WARNING:root:Binary data. Using default encoding. Possibly arg of unsupported operator: <class 'bytes'>

shrewd ermine
#

Looks like you need to actually load the page then render it.

open maple
#

If you can help me with that

shrewd ermine
#

I've never used the module before sorry.

#

it seems straightforward though

#

that example navigates to page 8 then renders it

open maple
#

Yes but like what senetcne

#

Did the code work for you

shrewd ermine
#

You said you wanted to show the page where you find the word?

#

I haven't run any code.

open maple
#

Okay

shrewd ermine
#
  1. find the page with the word. 2. navigate to that page and render it
#

idk if you can like highlight it or anything with the module

#

the api reference is at that website though so you can poke around

open maple
#

Okay as thank you as I done this before but cannot remember without seeing my repvious code at work

shrewd ermine
#

Well that tutorial seems pretty thorough so I think if you spend some time with it you'll figure it out.

open maple
#

Okay

open maple
#

@shrewd ermine

#

Founded this code but I am getting an idnex out of range ror the pageNumbers

#
import PyPDF2
import re
import glob
 
file_list = glob.glob("/Users/kevineldridge/Documents/*.pdf")
failed_pages = []
 
# define search string
String = input("Search: ")
 
for j in file_list:
    # open the pdf file
    print('-'*100)
    print("Opening: \n"+str(j)+"\n")
    obj = PyPDF2.PdfReader(j)
    # get number of pages
    NumPages = len(obj.pages) 
    failed_pages = []
    # extract text and do the search
    for i in range(0, NumPages):
        PageObj = obj.pages[i]
        try:
            Text = PageObj.extractText()
        except:
            Text = ''
            failed_pages.append(i)
            pass
        ResSearch = re.search(String, Text)
        if ResSearch:
            print("Found on page: "+str(i)+" "+(str(Text[ResSearch.start()-30:ResSearch.end()+30])).replace("\n", " "))
    print("Failed pages:"+str(failed_pages))
input("prompt: ")
shrewd ermine
#

Where did you find it?

shrewd ermine
#

ah

#

Why not try to write it yourself? That uses an entirely different module.

open maple
#

I will do that as it does make sense

#

for i in range(0, NumPages):
PageObj = obj.pages[i]
try:
Text = PageObj.extractText()

open maple
#

!close

fresh belfryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.