python使用SpeechLib完成文本转换为语音

使用speechLib读取文本转成语音文件存储
首先导入: pip install comtypes
代码如下:
from comtypes.client import CreateObject
engine=CreateObject(“SAPI.SpVoice”);
stream=CreateObject(‘SAPI.SpFileStream’)
from comtypes.gen import SpeechLib
infile=‘yuyin.txt’
outfile=‘yuyin.wav’
stream.Open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream=stream
f=open(infile,‘r’,encoding=‘utf-8’)
theText=f.read()
f.close()
engine.speak(theText)
stream.close()

你可能感兴趣的:(python)