0

I am building a pdf reader and it works just fine. But, I can't pause it. If it starts talking in never stops until the entire text is read. So, how can I pause and resume (or even stop) the audio whenever I want?

Here is my code:

import pyaudio
import pyttsx3
import PyPDF2
import keyboard   

book = open(r"C:\Users\M E T R O\OneDrive\Desktop\self-motivational books\the_richest_man_in_babylon.pdf", "rb")
pdfreader = PyPDF2.PdfReader(book)
pages = pdfreader.numPages
speaker = pyttsx3.init()
speaker.setProperty("rate", 140)

for i in range(4, pages):
    page = pdfreader.getPage(i)
    text = page.extractText()
    speaker.say(text)
    speaker.runAndWait()
Chandler Bong
  • 461
  • 1
  • 3
  • 18
  • you would have to split all text to separate words and `say()` every word separatelly - and then you may add code which check some boolean variable - ie. `pause = True/False` - to pause it. But when it will read separated words then it may sounds strange. – furas Sep 14 '22 at 12:31
  • @furas I have already tried this but the voice was really strange some words i think were not properly pronounced – Chandler Bong Sep 14 '22 at 12:33
  • you can see [source code](https://github.com/nateshmbhat/pyttsx3) and it doesn't have function to pause it. It has function `.stop()` but you can't restart it and you can't start at some place. Eventually you may try to write audio to file and use external audio player for this. – furas Sep 14 '22 at 12:51

0 Answers0