title: Loading a PDF file into a program using a tkinter GUI
body: <p>The program I am writing is able to take in a PDF, find all the non-stop words in the PDF, display all of those words in a table as well as how often they appeared in the PDF, and then display that table in a web browser. So far, the program has been able to execute this when the PDF that is being read is located in the same file as the program that is being executed. I want to make my code more stream-lined, so a user can decide what PDF the program will read, regardless of where the PDF actually is. To do this, I attempted to use tkinter, because all other GUI are unavailable for me to use, and I can get the window and the buttons necessary to appear, as well as the file explorer to open, however I can't figure out how to actually have the code exectue when I "double-click" the PDF I want the code to read.</p>
  
  <pre><code>import word_bag_GUI
  import PyPDF2
  import pandas
  import webbrowser
  import os
  from nltk.tokenize import RegexpTokenizer
  from nltk.corpus import stopwords
  from nltk.stem import PorterStemmer
  from nltk.stem.wordnet import WordNetLemmatizer
  from nltk.tokenize import word_tokenize
  
  #Method that a pdf that is read into the program goes through to eliminate any unwanted words or symbols#
  def preprocess(text):
      #Filters out punctuation from paragraph witch becomes tokenized to words and punctuation#
      tokenizer = RegexpTokenizer(r'\w+')
      result = tokenizer.tokenize(text)
  
      #Makes all words lowercase#
      words = [item.lower() for item in result]
  
      #Removes all remaining tokens that are not alphabetic#
      result = [word for word in words if word.isalpha()]
  
      #Imports stopwords to be removed from paragraph#
      stop_words = set(stopwords.words("english"))
  
      #Removes the stop words from the paragraph#
      filtered_sent = []
      for w in result:
          if w not in stop_words:
              filtered_sent.append(w)
  
      #Return word to root word/chop-off derivational affixes#
      ps = PorterStemmer()
      stemmed_words = []
      for w in filtered_sent:
          stemmed_words.append(ps.stem(w))
  
      #Lemmatization, which reduces word to their base word, which is linguistically correct lemmas#
      lem = WordNetLemmatizer()
      lemmatized_words = ' '.join([lem.lemmatize(w,'n') and lem.lemmatize(w,'v') for w in filtered_sent])
  
      #Re-tokenize lemmatized words string#
      tokenized_word = word_tokenize(lemmatized_words)
      return tokenized_word
  
  #Loads in PDF into program#
  PDF_file = word_bag_GUI.open_PDF
  read_pdf = PyPDF2.PdfFileReader(PDF_file)
  
  #Determines number of pages in PDF file and sets the document content to 'null'#
  number_of_pages = read_pdf.getNumPages()
  doc_content = ""
  
  #Extract text from the PDF file#
  for i in range(number_of_pages):
      page = read_pdf.getPage(0)
      page_content = page.extractText()
      doc_content += page_content
  
  #Turns the text drawn from the PDF file into data the remaining code can understand#
  tokenized_words = preprocess(doc_content)
  
  #Determine frequency of words tokenized + lemmatized text#
  from nltk.probability import FreqDist
  fdist = FreqDist(tokenized_words)
  final_list = fdist.most_common(len(fdist))
  
  #Organize data into two columns and export the data to an html that automatically opens#
  df = pandas.DataFrame(final_list, columns = ["Word", "Frequency"])
  df.to_html('word_frequency.html')
  webbrowser.open('file://' + os.path.realpath('word_frequency.html'))
  
  -------------------------------------------------------------------------
  
  #Creats the GUI that will be used to select inputs#
  window = tk.Tk()
  window.geometry("300x300")
  window.resizable(0,0)
  window.title("Word Frequency Program")
  
  #Browse through file directory and select PDF to be used in code#
  def open_PDF():
      filedialog.askopenfile(initialdir = "/",title = "Select file",filetypes = (("PDF files","*.pdf"),("all files","*.*")))
  button1 = ttk.Button(window, text = "Browse Files", command = open_PDF)
  button1.grid()
  
  #Quits out of the program when certain button clicked#
  button2 = ttk.Button(window, text = "Quit Program", command = window.quit)
  button2.grid()
  
  window.mainloop()
  window.destroy()
  </code></pre>
  
  <p>I expected the program to execute the same way before I implemented a tkinter GUI, printing out a table with the words and frequency in a web browser, however when I select and open the PDF I want the program to read, nothing happens.</p>
  
  <p>Edit: I seem to have gotten some portion of it to work, however now I'm getting an exception which states: </p>
  
  <pre><code>TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
  </code></pre>
  
  <p>Edit 2: I am now getting the error:</p>
  
  <pre><code>AttributeError: 'function' object has not attribute 'seek'
  </code></pre>
  
  <p>and the only change I have made was changing the open_PDF() method as such:</p>
  
  <pre><code>def open_PDF():
      filename = filedialog.askopenfile(initialdir = "/", title = "Select file", filetypes = (("PDF files","*.pdf"), ("all files","*.*")))
      return filename
  </code></pre>