我正在尝试将python脱机文本用于语音库pyttsx3。当我运行以下脚本时:
import pyttsx3
engine = pyttsx3.init('sapi')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say("hello friends")
我收到此错误:
C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/Hp/PycharmProjects/miniproject/demo12.py
Traceback (most recent call last):
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\weakref.py", line 131, in __getitem__
o = self.data[key]()
KeyError: 'sapi'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/miniproject/demo12.py", line 4, in <module>
engine = pyttsx3.init('sapi')
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
self._module = importlib.import_module(name)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pyttsx3.drivers.sapi'
答案 0 :(得分:1)
engine = pyttsx3.init('sapi5')
键入此内容而不是您键入的内容
希望它能正常工作,并且在engine.say()
之后,您必须输入
engine.runAndWait()