I am using the pyttsx3 module for my Alexa-like project. The source code I wrote all worked fine on my own PC, but now I am trying to get this thing to work on a 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')
Nothing special but when I run this code on a 64-bit based Python version I get this error:
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
What caught my eye was the last line: OSError: [WinError -2147024703] %1 is not a valid Win32 application
So I tried running the exact same program with a 32bit Python version. This worked. But I need this to work on 64bit because I have some other modules installed in my real program that else won't work properly.
I also tried to uninstall pytts3x and install the 2.71 version with pip install pyttsx3==2.71
, this gave me a pywintypes_com error.
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)
I tried this solution to solve this pywintypes error, but no luck.
What more can I try?
When I installed Raspbian (32bit) on the Pi, I, unfortunately, got the same error. I think Pyttsx3 is a really good module but not for the Pi. So I did some research to find another Text-to-Speech (TTS) module for Python. I came across gTTS, which is also a great module and easy to integrate. It uses Google translate, so you can choose from each Google Translate language.
This is my workaround for now. If anyone has an answer to why pyttsx3 ain't working on a Pi, let me know. Although gTTS is great, I like Sapi5 more.
User contributions licensed under CC BY-SA 3.0