0

Hello i want to make a pdf reader but there's an error occures named "expected str, bytes or os.PathLike object, not TextIOWrapper" here is the codes

import PyPDF2
import pyttsx3
from tkinter import * 
from tkinter.filedialog import askopenfile
from pygame import mixer_music
root = Tk()
root.geometry('800x600')
root.title("PDF Reader")
book = askopenfile()
pdfobj = open(book, 'rb')
#error occures here
pfreader = PyPDF2.PdfReader(pdfobj)
def read_pdf():
    pdfobj = open(book, 'rb')
    pfreader = PyPDF2.PdfReader(pdfobj)

text = ""

for pagenumber in range(len(pfreader.pages)):
    page = pfreader.pages[pagenumber]
    text += page.extract_text()

def say_pdf():
    engine = pyttsx3.init()
    engine.say(pagenumber)
    print(text)

l1 = Label(text = text, height = 12)
l1.grid(column = 1)
choose_file = Button(text = 'Choose a file', command = read_pdf)
choose_file.grid(column = 2)
read_file = Button(text = 'Read', command = say_pdf)
read_file.grid(column = 3)

I dont understand the error and cant solve it i hope you can help me

1 Answers1

0

The open function expects a str, bytes or os.PathLike object. In the code above you are trying to open the result of askopenfile.

Just use:

book = askopenfile(mode="rb") # specify the read binary mode
pfreader = PyPDF2.PdfReader(book)

See https://docs.python.org/3/library/dialog.html#tkinter.filedialog.askopenfile.

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
  • But there's another error occured that is "can't do nonzero cur-relative seeks" can you pls help me and im sorry about my ignorance im just new in python – DIdar Babishow Mar 05 '23 at 13:35
  • It could be related on opening mode (by default not binary for askopenfile function). See the updated answer. – Giancarlo Romeo Mar 05 '23 at 15:29