[英]Saving Text to Speech Python
I am trying save the results of text to speech to a file on windows.我正在尝试将文本到语音的结果保存到 Windows 上的文件中。 I successfully gotten it to speak (using
speak.Speak
).我成功让它说话(使用
speak.Speak
)。 However, no such luck with saving files.但是,保存文件没有这样的运气。
The problem is AudioOutputStream
isn't being found, despite itbeing listed in the Microsoft docs .问题是
AudioOutputStream
没有被找到,尽管它被列在 Microsoft docs 中。
Version information: Windows 10, Python 3.6版本信息: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()
I managed to get it working.我设法让它工作。 As there was little response to this post originally, I believe it would be very useful to leave this fix for future visitors.
由于最初对这篇文章的回应很少,我相信将这个修复程序留给未来的访问者会非常有用。
Use pip install comtypes
to install the library.使用
pip install comtypes
安装库。 It is a lot more cooperative and less janky than python's native comtype implementation.它比 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()
I found this question but the solution provided by Neil using comtypes didn't work for me and gave me an error.我发现了这个问题,但是 Neil 使用 comtypes 提供的解决方案对我不起作用,并给了我一个错误。
However, his original solution did work on my system:但是,他的原始解决方案确实适用于我的系统:
I used this to create .wav files for a speaking clock我用它来为讲话时钟创建 .wav 文件
import win32com.client
phrases = [
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
"Twenty",
"Thirty",
"Fourty",
"Fifty",
"Hundred",
]
speaker = win32com.client.Dispatch("SAPI.SpVoice")
filestream = win32com.client.Dispatch("SAPI.SpFileStream")
def speak(phrase):
speaker.speak(phrase)
def save_voice(phrase):
filestream.open(".".join([phrase, "wav"]), 3, False)
speaker.AudioOutputStream = filestream
speaker.speak(phrase)
filestream.close()
for phrase in phrases:
save_voice(phrase)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.