我正在尝试将文本转语音的结果保存到 Windows 上的文件中。我成功让它说话了(使用 speak.Speak
)。然而,保存文件就没这么幸运了。
问题是尽管找到了 AudioOutputStream
being listed in the Microsoft docs .
版本信息:Windows 10、Python 3.6
错误
Traceback (most recent call last):
File "...\dd.py", line 87, in <module>
speak.AudioOutputStream = filestream
File "...\win32com\client\dynamic.py", line 565, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)
代码
from win32com.client import Dispatch
import win32api
speak = Dispatch("SAPI.SpVoice")
filestream = Dispatch("SAPI.SpFileStream")
filestream.open("out.wav", 3, False)
for k in speak.GetAudioOutputs():
print(k.GetDescription())
speak.AudioOutputStream = filestream
speak.Speak("test")
filestream.close()
最佳答案
我设法让它工作了。由于最初对这篇文章的回应很少,我相信将此修复留给 future 的访问者会非常有用。
使用pip install comtypes
安装库。与 python 的原生 comtype 实现相比,它更加协作且不那么卡顿。
import comtypes.client
speak = comtypes.client.CreateObject("SAPI.SpVoice")
filestream = comtypes.client.CreateObject("SAPI.spFileStream")
filestream.open("out.wav", 3, False)
speak.AudioOutputStream = filestream
speak.Speak("test")
filestream.close()
关于python - 将文本保存为语音 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49871252/