如何更改pyttsx3中的声音?

时间:2017-07-01 06:43:15

标签: python python-3.x text-to-speech speech-synthesis pyttsx

此代码正常运行,但我只能在Microsoft Windows中预先安装的声音之间切换。这些声音是“Microsoft David Mobile”和“Microsoft Zira Mobile”。

后来我安装了“Microsoft Kalpana Mobile”并将其设置为默认的Windows语音。但我仍然无法切换到“Microsoft Kalpana Mobile”。代码是 -

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()

voices [] 中只有0和1作为索引。

我想要“Microsoft Kalpana Mobile”发言。我在这个项目上工作了2个月。如果这不起作用,我所有的努力都将继续下去。请帮忙:(

提前致谢。

4 个答案:

答案 0 :(得分:8)

您可以尝试以下代码:

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()

然后选择你喜欢的voice.id

而不是for循环

答案 1 :(得分:1)

我刚才注意到了。设置语言⇓这只是我的默认语言设置是'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()

voice.name == 'Alex'

答案 2 :(得分:0)

voices = engine.getProperty('voices')

"voices" 是一个语音列表。所以我们需要确定我们想要设置的语音索引。 从 index = 0 开始。第一个语音

运行一个循环,你会看到带有索引和名称的声音

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
index = 0
for voice in voices:
   print(f'index-> {index} -- {voice.name}')
   index +=1
engine.runAndWait()

然后只需为第四个声音设置索引“voices[3].id”,例如索引“3”

engine.setProperty('voice', voices[3].id)

完整的代码可能如下所示:

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.say("hello I am the voice from your PC")
engine.runAndWait()

答案 3 :(得分:0)

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.say("hello")
engine.runAndWait()

如果您已经有如下代码:

brain = "hello"

你可以试试这个:

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(brain)
engine.runAndWait()