0

通过以下代码在我的python 3.8.5虚拟环境中使用pyttsx3模块时:

import pyttsx3

def speak(speak):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.getProperty('rate')
    engine.setProperty('rate', 155)
    engine.say(speak)
    engine.runAndWait()

speak("Hello")

我在编译上述代码时收到此错误:

Traceback (most recent call last):
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "c:\users\CurrentUser\appdata\local\programs\python\python38-32\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 "d:/virtualenv/hehe.py", line 12, in <module>
    speak("Hello")
  File "d:/virtualenv/hehe.py", line 4, in speak
    engine = pyttsx3.init()
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "c:\users\CurrentUser\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 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 1, in <module>
    import comtypes.client  # Importing comtypes.client will make the gen subpackage
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1176, in <module>
    class IPersist(IUnknown):
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1180, in IPersist
    COMMETHOD([], HRESULT, 'GetClassID',
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1099, in COMMETHOD
    from comtypes.automation import VARIANT
  File "D:\virtualenv\venv\lib\site-packages\comtypes\automation.py", line 4, in <module>
    import decimal
  File "c:\users\CurrentUser\appdata\local\programs\python\python38-32\lib\decimal.py", line 3, in <module>
    from _decimal import *
AttributeError: module 'numbers' has no attribute 'Number'

每次我运行给定的 python 代码时,都会发生上述错误,我在 Windows 10 中使用 VS 代码作为我的编码 IDE

4

2 回答 2

0

这应该有效:

import pyttsx3
engine = pyttsx3.init()

def speak(word):
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    rate = engine.getProperty('rate')
    engine.setProperty('rate', 155)
    engine.say(word)
    engine.runAndWait()

speak("Hello")
于 2020-10-15T13:18:08.540 回答
0

退出pyttsx3.init()函数。它不是重复调用。它只能被调用一次。

import pyttsx3
engine = pyttsx3.init()
def speak(word):
   #set engine properties
   engine.say(word)
   engine.runAndWait()

speak('Speak')
于 2020-10-15T05:04:04.923 回答