【发布时间】:2020-04-23 07:39:47
【问题描述】:
在 python 中使用 Pyttsx 模块时,如何更改播放文本时使用的语音 ID?
所提供的文档说明了如何循环显示所有可用的声音,但没有明确说明如何选择特定的声音。
【问题讨论】:
在 python 中使用 Pyttsx 模块时,如何更改播放文本时使用的语音 ID?
所提供的文档说明了如何循环显示所有可用的声音,但没有明确说明如何选择特定的声音。
【问题讨论】:
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()
【讨论】:
嗯,你应该使用engine.setProperty('voice', voice_id)(voice_id 是系统中语音的 ID;你可以从engine.getProperty('voices') 获取可用语音列表),如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()
您不必循环,您可以在没有for 循环的情况下设置语音ID。
就这样做吧:
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.')
【讨论】:
这里是一个pyttsx3模块使用示例:
import pyttsx3
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
speak('Hello World')
请参阅docs 了解更多信息。
【讨论】: