1

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())
1
  • Sorry, it is not really clear what you are trying to do, nor in how far what you have doesn't do that. It might help to build a minimal reproducible example that does not rely on PDFs and GUIs to run – your description implies a simple function reading e.g. time.time() might be enough. At any rate, consider to do the Python tutorial to get familiar with the terms used to describe code. Commented Feb 7, 2021 at 19:38

1 Answer 1

1

There are two things to improve here, first, if you want to save the result of a function you can just save it to a variable like this

result = key_search()
print(result)

The second, notice that when you return from a function, it quits the function in that moment. Let's say that reader.pages has 5 pages, your function currently reads the first one and quits. If you want to read all of them you first need to save the results to a list then return the list

results = []
for page in reader.pages:
    results.append(page.extractText())
return results # Notice you dont need parentheses here

Then you can print all the results

pages = key_search()
for page in pages:
    print(page)
  • There are more standard/efficient ways to do those things, but since you are just beginning it's better to see things clearer like that
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I actually have another, general, question. In the context of this code what is "page" ? I never created a variable called "page" so I don't know what it is related to or how my code recognizes what it is.
This is pretty basic so I'm sure you will find better explanations if you just search for for loops in python. But in short pages is iterable, which means you can iterate through it. So in this case you are telling the code to go through the list of pages, and for each item in the list assign it to a variable called page. Most other languages call it foreach. realpython.com/python-for-loop/#the-python-for-loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.