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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically