当前位置:   article > 正文

Python中使用三方库SpeechLib实现文本转换语音文件的示例(Win10)

Python中使用三方库SpeechLib实现文本转换语音文件的示例(Win10)

一、需要安装的三方库:pip install comtypes

二、示例代码:

  1. from comtypes.client import CreateObject
  2. from comtypes.gen import SpeechLib
  3. engine = CreateObject('SAPI.SpVoice')
  4. stream = CreateObject('SAPI.SpFileStream')
  5. infile = 'E:\\计算机科学速成课\\18. 操作系统-Operating Systems.txt'
  6. outfile = 'E:\\demo.wav'
  7. stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
  8. engine.AudioOutputStream = stream
  9. with open(infile, 'r', encoding='utf-8') as file:
  10. text = file.read()
  11. engine.speak(text)
  12. stream.close()

三、要点注释:

开始用创建对象的方法CreateObject 分别创建了语音引擎对象与语音文件流对象。调用流对象的Open()方法实现语音流数据写入音频输出文件。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/838591
推荐阅读
相关标签
  

闽ICP备14008679号