Changing the voice with PYTTSX module in python

允我心安 提交于 2019-11-28 02:20:26

问题


When using the Pyttsx module within python, how do you change the voice ID that is used when playing out text?

The documentation provided illustrates how to cycle through all the available voices, but does not make clear how to choose a specific one.


回答1:


Uh, you should use engine.setProperty('voice', voice_id) (with voice_id being an ID of the voice in your system; you can grab the list of available voices from engine.getProperty('voices')) as proposed in that example:

engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)  # changes the voice
   engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

You don't have to cycle, you can set voice id without a for loop.
Just do it like that:

engine = pyttsx.init()
engine.setProperty('voice', voice_id)  # use whatever voice_id you'd like
engine.say('The quick brown fox jumped over the lazy dog.')



回答2:


import pyttsx

engine = pyttsx.init()
voices = engine.getProperty('voices')

engine.setProperty('voice', voices[0].id) #change index to change voices
engine.say('I\'m a little teapot...')

engine.runAndWait()


来源:https://stackoverflow.com/questions/28344200/changing-the-voice-with-pyttsx-module-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!