1

so i made a program that can convert pdf file to text bu then I make it into a gui so that its easier for a user but when it work I wasn't able to copy the text from the text box is there something wrong with my code or did I do something wrong pls help

import PyPDF2
import tkinter as tk
from tkinter import filedialog
from tkinter import *

def browseFiles():
    file = filedialog.askopenfilename(initialdir = "/",title = "Select a Pdf File", filetypes = (("PDF files","*.pdf"),("all files","*.*")))
    print(file)
    return file


#Graphical User Interface
root = tk.Tk()
root.title('Pdf to text')
root.geometry('600x400')


#Upload pdf


tLabel = tk.Label(root, text='PDF to Text Converter')
tLabel.pack()

textFile = tk.Text(root, height=20, width=70)
textFile.config(bg='#dddddd')
textFile.pack()

button_explore = Button(root,
                        text = "Browse Files",
                        command = browseFiles,)
button_explore.pack()

button_exit = Button(root,
                     text = "  Exit  ",
                     command = exit)
button_exit.pack()

file = browseFiles()
pdfFileObj = open(file, "rb")

    # create reader variable that will read the pdffileobj
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

    # This will store the number of pages of this pdf file
x = pdfReader.numPages

    # create variable that will select the selected number of pages
    # x+1 because python indentation start from 0
pageObj = pdfReader.getPage(x - 1)

    # create text variable which will store all text data from pdf file
textObj = pageObj.extractText()

textFile.config(state='normal')
textFile.delete('1.0', 'end')
textFile.insert('1.0', textObj)
textFile.config(state='disabled')
    
    
root.mainloop()
  • Which text box? `textFile`? What method are you using to copy? keyboard shortcuts? – scotty3785 Sep 03 '21 at 12:27
  • Your `file = browseFiles()` line is almost certainly in the wrong place. Shouldn't browseFiles only be called when you press the "Browse Files" button. – scotty3785 Sep 03 '21 at 12:29
  • keyboard shortcuts – vincent owen Sep 03 '21 at 12:30
  • and yes it immediately open the browse files where should the correct place be ? – vincent owen Sep 03 '21 at 12:31
  • Can you be a bit more specific? Which keyboard short cuts? Perhaps you can edit your answer to explain exactly what buttons/keys you press when your program runs that leads to the problem you are experiencing. This will allow someone to replicate the issue. Also which OS? – scotty3785 Sep 03 '21 at 12:31
  • im using windows OS and i use ctrl + c – vincent owen Sep 03 '21 at 12:32
  • The call to browseFiles should be inside a function called when you press the "Browse Files" button. This function should probably also contain all the code that reads the text from the PDF file so that this doesn't all happen immediately when you open the GUI – scotty3785 Sep 03 '21 at 12:33
  • so i should make a new function ? – vincent owen Sep 03 '21 at 12:39
  • oh wait i think i fix it for the browse files but now about the cant copy – vincent owen Sep 03 '21 at 12:40
  • did you read the [docs](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html) or even search for [answers](https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard) on this site? also don't import everything from a module (don't use `*` when importing besides you have already imported `tkinter`, why do it again?) – Matiiss Sep 03 '21 at 12:42
  • maybe you can't copy because you set it `disabled`. OR you have to first select text to copy it. – furas Sep 03 '21 at 13:29
  • on Linux I have to select text to copy it. To copy without selecting it would need to create special function for this and assing to Ctrl+C. – furas Sep 03 '21 at 13:34
  • Thank you all i think i was able to fix my work – vincent owen Sep 03 '21 at 14:01

0 Answers0