pyttsx3 智能语音出错

python3.8 pyttsx3 初始化出错
OSError: [WinError -2147221005] 无效的类字符串

# -*- coding:utf8 -*-
import pyttsx3

k = pyttsx3.init()
k.say('我是谁?')
k.runAndWait()
Traceback (most recent call last):
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "D:\Program Files (x86)\Python\Python38\lib\weakref.py", line 131, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in 
    from comtypes.gen import SpeechLib  # comtypes
ImportError: cannot import name 'SpeechLib' from 'comtypes.gen' (E:\python文件3.8\venv\lib\site-packages\comtypes\gen\__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/python文件3.8/实验1.py", line 4, in 
    k = pyttsx3.init()
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "D:\Program Files (x86)\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1014, in _gcd_import
  File "", line 991, in _find_and_load
  File "", line 975, in _find_and_load_unlocked
  File "", line 671, in _load_unlocked
  File "", line 783, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "E:\python文件3.8\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 6, in 
    engine = comtypes.client.CreateObject("SAPI.SpVoice")
  File "E:\python文件3.8\venv\lib\site-packages\comtypes\client\__init__.py", line 204, in CreateObject
    clsid = comtypes.GUID.from_progid(progid)
  File "E:\python文件3.8\venv\lib\site-packages\comtypes\GUID.py", line 86, in from_progid
    _CLSIDFromProgID(text_type(progid), byref(inst))
  File "_ctypes/callproc.c", line 948, in GetResult
OSError: [WinError -2147221005] 无效的类字符串

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

望采纳


这个错误的原因是你的 pyttsx3 模块没有正确安装。你可以尝试以下操作来解决这个问题:


确保你已经安装了 pyttsx3 模块,可以在命令行中输入 "pip install pyttsx3" 来安装它。


如果你已经安装了 pyttsx3 模块,但是仍然遇到了上述错误,可以尝试重新安装该模块,命令如下:

pip uninstall pyttsx3
pip install pyttsx3

如果仍然无法解决问题,可以尝试更新你的 Python 版本,因为 pyttsx3 模块可能需要较新的 Python 版本来运行。