我将 pyttsx3 模块用于类似 Alexa 的项目。我编写的源代码在我自己的 PC 上运行良好,但现在我正试图让这件事在 Pi 上运行。
import pyttsx3
engine = pyttsx3.init()
nl_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\MSTTS_V110_nlNL_Frank" # Dutch Voice..
voices = engine.getProperty('voices')
engine.setProperty('voice', nl_voice_id)
def talk(text):
engine.say(text)
engine.runAndWait()
talk('test')
没什么特别的,但是当我在基于 64 位的 Python 版本上运行此代码时,我收到此错误:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\weakref.py", line 134, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in <module>
from comtypes.gen import SpeechLib # comtypes
ImportError: cannot import name 'SpeechLib' from 'comtypes.gen' (C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\comtypes\gen\__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\Documents\user\user-master\userv2\user\Static\test.py", line 3, in <module>
engine = pyttsx3.init()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
self._module = importlib.import_module(name)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\drivers\sapi5.py", line 6, in <module>
engine = comtypes.client.CreateObject("SAPI.SpVoice")
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\comtypes\client\__init__.py", line 238, in CreateObject
obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\comtypes\__init__.py", line 1219, in CoCreateInstance
_ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
File "_ctypes/callproc.c", line 1010, in GetResult
OSError: [WinError -2147024703] %1 is not a valid Win32 application
吸引我眼球的是最后一行:OSError: [WinError -2147024703] %1 is not a valid Win32 application
所以我尝试使用 32 位 Python 版本运行完全相同的程序。这奏效了。但是我需要它在 64 位上工作,因为我在我的真实程序中安装了一些其他模块,否则这些模块将无法正常工作。
我还尝试卸载 pytts3x 并使用 pip install pyttsx3==2.71
安装 2.71 版本,这给了我一个 pywintypes_com 错误。
Traceback (most recent call last):
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\weakref.py", line 134, in __getitem__
o = self.data[key]()
KeyError: 'sapi5'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Kali\Documents\Kali\Kali-master\Kaliv2\Kali\Static\test.py", line 3, in <module>
engine = pyttsx3.init("sapi5")
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\driver.py", line 52, in __init__
self._driver = self._module.buildDriver(weakref.proxy(self))
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\drivers\sapi5.py", line 23, in buildDriver
return SAPI5Driver(proxy)
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\drivers\sapi5.py", line 28, in __init__
self._tts = win32com.client.Dispatch('SAPI.SPVoice')
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\Kali\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147024703, 'OLE error 0x800700c1', None, None)
我尝试了 this solution 来解决这个 pywintypes 错误,但没有成功。
我还能尝试什么?
当我在 Pi 上安装 Raspbian(32 位)时,不幸的是,我遇到了同样的错误。我认为 Pyttsx3 是一个非常好的模块,但不适用于 Pi。所以我做了一些研究,以找到另一个适用于 Python 的文本到语音 (TTS) 模块。我遇到了 gTTS,它也是一个很棒的模块并且易于集成。它使用 Google 翻译,因此您可以从每种 Google 翻译语言中进行选择。
这是我目前的解决方法。如果有人对为什么 pyttsx3 不在 Pi 上工作有答案,请告诉我。虽然 gTTS 很棒,但我更喜欢 Sapi5。