Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python comtypes文本到语音以静默方式失败_Python_Python 2.7_Text To Speech_Comtypes - Fatal编程技术网

Python comtypes文本到语音以静默方式失败

Python comtypes文本到语音以静默方式失败,python,python-2.7,text-to-speech,comtypes,Python,Python 2.7,Text To Speech,Comtypes,我正在使用以下代码将文本语音音频保存到文件: from comtypes.client import CreateObject from comtypes.gen import SpeechLib engine = CreateObject("SAPI.SpVoice") stream = CreateObject("SAPI.SpFileStream") stream.Open('audio.mp3', SpeechLib.SSFMCreateForWrite) engine.AudioOu

我正在使用以下代码将文本语音音频保存到文件:

from comtypes.client import CreateObject
from comtypes.gen import SpeechLib

engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
stream.Open('audio.mp3', SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
有时,创建的文件无法播放,并且可能大小为零-尽管不会发生错误。我发现了一个输入文本的例子,在其中一次失败中使用了它(太长了,这里不包括),结果是可复制的,但它没有明显的问题特征


我无法找出如何检测上述代码中的错误,也无法确定导致问题的原因。理想情况下,我希望解决这个问题,但检测问题以使我能够处理它就足够了。我怎样才能做到这一点呢?

我不知道为什么它会悄无声息地失败,但当我运行它时,它提出:
导入错误:无法导入名称SpeechLib

然后我去了trusty ole Google,发现了这个问题:

因此,您的代码应该如下所示:

from comtypes.client import CreateObject

text="Hi there, how are you?"
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open('audio.mp3', SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()

这起作用并保存了audio.mp3文件。

嗯,
text
没有定义。我假设它在您的代码中?是的,
文本
在我的代码中定义-这是一个片段。感谢您的输入。我的代码在99%以上的时间内运行良好,不会产生您描述的错误。但是,有时音频文件会损坏,这就是我不知道如何修复的问题。回想起来,我记得我在第一次安装comtypes库时确实使用了“问答你”链接。但是,在运行了一次代码之后,我可以像我的问题中那样毫无错误地使用代码。我得到了类似的错误,通过安装特定版本
pip install pyttsx3==2.6