python 文本转语音问题

import sys

reload(sys)
sys.setdefaultencoding('utf8')
import pyttsx
engine = pyttsx.init()
engine.say('hello world')
engine.runAndWait()

当我使用pyttsy 尝试转换输出语音是时 出现了这样的错误

C:\Python27\python.exe D:/python/ccc.py
Traceback (most recent call last):
  File "D:/python/ccc.py", line 6, in <module>
    engine = pyttsx.init()
    
  File "C:\Python27\lib\site-packages\pyttsx\__init__.py", line 39, in init
    eng = Engine(driverName, debug)
   
  File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
    
  File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 66, in __init__
    self._driver = self._module.buildDriver(weakref.proxy(self))
    
  File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 37, in buildDriver
    return SAPI5Driver(proxy)
    
  File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 41, in __init__
    self._tts = win32com.client.Dispatch('SAPI.SPVoice')
    
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
    
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName

    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch

    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, '\xce\xde\xd0\xa7\xb5\xc4\xc0\xe0\xd7\xd6\xb7\xfb\xb4\xae', None, None)

请问这是什么原因呢?

阅读 4.1k
1 个回答

pyttsx 应该是调用 win32com.client.
这个模块比较有趣, 可以直接获取 windows 下很多程序的控制权.
比如操作EXCEL:

excel_app = win32com.client.Dispatch('Excel.Application')
excel_app.Visible = True    # 使 EXCEL 程序可见
excel_app.Worksheets(1).Cells(1, 1).Value = 'Something' # 设置单元格(A,1)内容

而 pyttsx 作为 win32com.client 的一个代理, 是调用了微软的RRSApp

self._tts = win32com.client.Dispatch('SAPI.SPVoice')

报错pywintypes.com_error: (-2147221005, '\xce\xde\xd0\xa7\xb5\xc4\xc0\xe0\xd7\xd6\xb7\xfb\xb4\xae', None, None)意思是"无效的字符串", 这个错误说明程序SAPI.SPVoice不能被识别。推测应该是题主的 windows 尚未安装 Speech SDK.

参考Where can I download the SAPI 5.3 SDK?, 应该去微软的官网看看有没有合适的 Speech SDK 下载安装.

另外, Use Python for Windows for SAPI5 speech中提到

If necessary, download and install Microsoft [SAPI5SpeechInstaller.msi](https://www.microsoft.com/en-us/download/details.aspx?id=10121). Current Windows systems include speech by default, and the current Windows Software Development Kit includes up-to-date speech components for programmers.

可以尝试一下下载这个Speech SDK.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题