Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method