This code is working but I'm only able to switch between the voices which came preInstalled in Microsoft Windows. These voices are "Microsoft David Mobile" and "Microsoft Zira Mobile".
Later I installed "Microsoft Kalpana Mobile" and set it as the default Windows voice. But still I'm not able to switch to "Microsoft Kalpana Mobile". The code is-
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #changing index changes voices but ony 0 and 1 are working here
engine.say('Hello World')
engine.runAndWait()
Only 0 and 1 are working as indices inside voices[].
I want the "Microsoft Kalpana Mobile" to speak. I'm working on this project for past 2 months. If this doesn't work, all my efforts will go in vein. Please Help:(
Thanks in advance.
You can try this code:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print(voice, voice.id)
engine.setProperty('voice', voice.id)
engine.say("Hello World!")
engine.runAndWait()
engine.stop()
Then instead of the for loop, just pick up your preferred voice.id
I have just noticed. To set the language⇓ This is just my default language setting is 'ja_JP'.
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print voice
if voice.languages[0] == u'en_US':
engine.setProperty('voice', voice.id)
break
engine.say('Hello World')
engine.runAndWait()
or
voice.name == 'Alex'
来源:https://stackoverflow.com/questions/44858120/how-to-change-the-voice-in-pyttsx3