1

我的私人助理代码:

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

# text to speech
def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()

显示错误:

Traceback (most recent call last):
  File "c:\Users\Samarth S\OneDrive\Desktop\AI Projects\AIVVA\3\main.py", line 18, in <module>
    engine.setProperty('voice', voices[0].id)
AttributeError: 'str' object has no attribute 'id'

这是我的第一个 python 项目,我对声音本身感到震惊。

4

1 回答 1

0

getProperty你很接近,问题特别是你用'voice'作为参数调用的这行代码:

voices = engine.getProperty('voice') 

相反,您应该getProperty使用“voices”作为参数调用:

voices = engine.getProperty('voices') 

使用“voices”调用getProperty将返回一个对象列表,pyttsx3.voice.Voice descriptor这些对象应该使这行代码工作:

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

查看pyttsx3 引擎文档了解详情。

于 2021-02-15T18:33:48.763 回答