Python Text to Speech | pyttsx module

Use this command to install:

 pip install pyttsx3 

Using —
First we need to import the library and then initialize it using the init () function. This function can take 2 arguments. 
init (driverName string, debug bool)

  • drivername: [Available driver name] sapi5 on Windows | NSSS on MacOS
  • debug: enable or disable debug output

After initialization, we will force the program to speak the text using the say () function. This method can also take 2 arguments. 
say (text unicode, name string)

  • text: any text you want to hear.
  • name: to set a name for this speech. (optional)

Finally, we use runAndWait () to start the speech. All say () texts will not be spoken unless the interpreter will encounter runAndWait () .

Code # 1: Speaking text

# pyttsx library import

import pyttsx3

# initialization

engine = pyttsx3.init ()

# testing

engine.say ( "My first code on text-to-speech " )

engine.say ( "Thank you, Geeksforgeeks" )

engine.runAndWait ()

Code # 2: listening for events

import pyttsx3


def onStart ():

print ( `starting` )


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)


sen = ` Geeks for geeks is a computer portal for Geeks`


engine.say (sen)
engine.runAndWait ()

Why pyttsx?
It works offline , unlike other text libraries. Instead of saving the text as an audio file, Pyttsx actually says it there. This makes it more reliable for use in voice projects.