一、修正SpeechLib的导入包顺序后的代码:
from comtypes.client import CreateObjectengine = CreateObject('SAPI.SpVoice')
stream = CreateObject('SAPI.SpFileStream')from comtypes.gen import SpeechLibinfile = 'E:\\语音文档\\易经64卦读音.txt'
outfile = 'E:\\demo.wav'stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
with open(infile, 'r', encoding='utf-8') as file:text = file.read()
engine.speak(text)
stream.close()
二、运行出现Typelib different than module的错误:
前一篇博文的代码中导入SpeechLib库的语句放到了最前面,头次运行代码无问题。再运行代码生成语音文件时就出现了此错误,提示意思是这个Typelib不同于模块的问题。SpeechLib改到了现在的位置,这样逻辑顺序才正确!就不再出现运行错误了!