首页 > 解决方案 > 想在 pyttsx3 中将声音更改为 Siri 男性

问题描述

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
    print (voice)
    if voice.languages[-1] == u'en_us':
        engine.setProperty('voice', voice.id)

engine.say('Hello World')
engine.runAndWait()

#the list docent have Siri included 

标签: pythonvoicesiripyttsx

解决方案


你这样设置声音:

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[i].id)

其中i是一个整数,指向您安装的 Siri 语音之一。

你可以用这个来测试不同的声音,例如:

import pyttsx3

text = "Greetings Professor Falken"
engine = pyttsx3.init()
voices = engine.getProperty('voices')

while True:
    try:
        user_input = input()
        i = int(user_input)
        engine.setProperty('voice', voices[i].id)
        engine.say(text)
        engine.runAndWait()
        print(user_input+") "+engine.getProperty('voice'))
    except Exception as e:
        print(e)

输入一个整数,然后按 Enter 听到声音。

或者,如果您想遍历所有已安装的声音,您可以这样做:

import pyttsx3

text = "Greetings Professor Falken"
engine = pyttsx3.init()
voices = engine.getProperty('voices')

for voice in voices:
    try:
        i = voices.index(voice)
        engine.setProperty('voice', voices[i].id)
        engine.say(text)
        engine.runAndWait()
        print(str(i)+") "+engine.getProperty('voice'))
    except Exception as e:
        print(e)

推荐阅读