Diduah
Diduah

Reputation: 1

expected str, bytes or os.PathLike object, not TextIOWrapper error

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

Upvotes: 0

Views: 204

Answers (1)

Giancarlo Romeo
Giancarlo Romeo

Reputation: 661

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.

Upvotes: 0

Related Questions