Jabed Khan
Jabed Khan

Reputation: 1

I want to listen to a pdf file as audio and my question is to run time.sleep() for 10 seconds and countdown through the speaker

Here is my code:

import pyttsx3 as audio_converter
import PyPDF2  # version 1.26.0
import time
import sys


book = open("OOP.pdf", "rb")
pdfreader = PyPDF2.PdfFileReader(book)

pages = pdfreader.numPages  # to find out number of pages
print(f'PDF name : "Object Oriented Programing (OOP)\nPage number : {pages}"')

speaker = audio_converter.init()  # to init the package of pyttsx3
page = pdfreader.getPage(7)  # pdf page number + 1 (index 0)
text = page.extractText()
speaker.say(f"Bismillah, Let's read the pdf {text}")

# pdf audio will start after the following commands
for i in range(5, 0, -1):
    sys.stdout.write(str(i)+' ')
    sys.stdout.flush()
    time.sleep(1)
speaker.runAndWait()  # this line will read existing pdf only

This code is made for read my pdf and i'm here to countdown the time underneath the for loop using speaker.say(#) i'm expecting here to speak the countdown time from 10 to 0 and that should be pronounced in speaker. in the very first line after the for loop,

sys.stdout.write(str(i)+' ')

when this line prints the countdown i want to use this comman such

speaker.say(str(i)) #but this is an error code

Please assist me

Upvotes: 0

Views: 149

Answers (1)

Gonzalo Odiard
Gonzalo Odiard

Reputation: 1355

Updating the response:

Trying to focus in the countdown, you need to call say() and after that runAndWait() to make the library play the sound

import pyttsx3 as audio_converter
import time
import sys

speaker = audio_converter.init()  # to init the package of pyttsx3

# pdf audio will start after the following commands
for i in range(5, 0, -1):
    sys.stdout.write(str(i)+' ')
    sys.stdout.flush()
    speaker.say(str(i))
    speaker.runAndWait()
    time.sleep(1)

After that, you can do the same to read the pdf text

Upvotes: 0

Related Questions