For a starter project, I am trying to write a piece of code that will open a GUI that can be used to select a PDF filepath that is then read and saved to a variable. Currently I am returning the results of a function and then trying to print the function. This must not be what I really want to do because when I print the function I am essentially running it again instead of just printing the returned string. Would the best solution be to save returned string to a variable within the function and then call that into the print statement later on?
I admittedly lack a basic understanding of what I am doing and nearly all of my code is frankensteined from fifty other posts made by other people. I'm sorry in advance for spamming the boards with a low level question like this, but I've been having trouble creating a search that finds an answer that I understand. Here is my code currently....
import PyPDF2
import tkinter
from tkinter import filedialog
root = tkinter.Tk()
root.withdraw()
def key_search():
filename = filedialog.askopenfilename(parent=root, title='Choose File Path') # Credit: https://stackoverflow.com/questions/3643418/storing-file-path-using-windows-explorer-browser-in-python
with open(filename, mode='rb') as f: #rb = read only binary, 'f' is a custom file object. Credit: https://www.soudegesu.com/en/post/python/extract-text-from-pdf-with-pypdf2/
reader = PyPDF2.PdfFileReader(f)
for page in reader.pages:
return(page.extractText())
print(key_search())