How to change voices in pyttsx3 modlue of python

3.3k views Asked by At

I am using the pyttsx3 module for text-to-speech in one of my python projects, but I am not able to choose a male/female option for voices. I read the documentation given on https://pypi.org/project/pyttsx3/ where it says use voices[0].id/voices[1].id for male and female voices respectively. However, that does not seem to work as there is no significant difference between the 2 voices.

My code:

import pyttsx3
engine = pyttsx3.init()

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

engine.say("Hello World!")
engine.runAndWait()

Any idea how to change the voice and also is there a way to change the language of text-to-speech...something similar to auto-translate?

2

There are 2 answers

0
Sairam Gourishetty On BEST ANSWER
voices = engine.getProperty('voices')       #getting details of current voice
#engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id)   #changing index, changes voices. 1 for female

this worked for me.

4
Ekure Edem On

To change the voice to either female or male, use

import pyttsx3 as p
engine = p.init()
voice = engine.getProperty('voice')
#for female
engine.setProperty(voice, "!v/f1")
#for male
engine.setProperty(voice, "!v/m1")
engine.runAndWait()