我正在考虑用python制作一个Siri / Cortana / JARVIS的东西,我有语音到文本和基本的AI工作,虽然我有文本到语音的问题。我在Windows 10和Python 3.6上。这是我的代码:
import win32com.client
class Voice(object):
def __init__(self, id, name=None, languages=[], gender=None, age=None):
self.id = id
self.name = name
self.languages = languages
self.gender = gender
self.age = age
def __str__(self):
return '''<Voice id=%(id)s
name=%(name)s
languages=%(languages)s
gender=%(gender)s
age=%(age)s>''' % self.__dict__
def _toVoice(attr):
return Voice(attr.Id, attr.GetDescription())
def _tokenFromId(id, speaker):
tokens = speaker.GetVoices()
for token in tokens:
if token.Id == id: return token
raise ValueError('unknown voice id %s', id)
class Zira():
def __init__(self):
self.speaker = win32com.client.Dispatch('SAPI.SpVoice')
self.voices = self.speaker.GetVoices()
self.voice = _toVoice(self.voices.Item(1))
self.speaker.Voice = _tokenFromId(self.voice.id, self.speaker)
def say(self, text):
self.speaker.Speak(text)
class David():
def __init__(self):
self.speaker = win32com.client.Dispatch('SAPI.SpVoice')
self.voices = self.speaker.GetVoices()
self.voice = _toVoice(self.voices.Item(0))
self.speaker.Voice = _tokenFromId(self.voice.id, self.speaker)
def say(self, text):
self.speaker.Speak(text)
我刚刚从Python 3.5迁移到3.6,它在3.5中运行良好,但现在不是3.6。我通过pip安装了win32。我的错误是:
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
n = Zira()
File "C:\Users\Public\Benjys_Python\xspeaker.py", line 15, in __init__
self.speaker.Voice = _tokenFromId(self.voice.id, self.speaker)
File "C:\Program Files\Python36\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)