The official documentation for pyttsx3 gives a variation of the following example for printing words that are currently being said. The only difference is that the print statements are in Python 3.x syntax instead of Python 2.x.
import pyttsx3
def onStart(name):
print('starting', name)
def onWord(name, location, length):
print('word', name, location, length)
def onEnd(name, completed):
print('finishing', name, completed)
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
The following incorrect output is printed.
starting None
word None 1 0
finishing None True
How can I print the actual word being uttered?
EDIT: If this task is not possible in pyttsx3, I am also open to using any other text to speech library to accomplish this.
The attribute
nameis intended to be a tag that is added to an utterance. You have to set it yourself as the second, optional argument tosay, for examplesay("hello world", "introduction"). In this case the value ofnamein all of the callbacks will beintroduction. From the documentation:You can use this by duplicating the actual text in the
engine.say()call, i.e.,engine.say(sentence, sentence). Then you can use the location and length arguments, which are string indexes, to extract the actual word from the sentence and print it in the callback.MCVE:
Output:
Note that each engine implements the callbacks separately. The above was tested with the
espeakengine on Linux, it might be that other engines for Windows and Mac implement it differently regarding the exposed information.