文本文件
代码
from comtypes.client import CreateObject
import win32com.client as win
try:
from comtypes.gen import SpeechLib # comtypes
except ImportError:
# Generate the SpeechLib lib and any associated files
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
speak = win.Dispatch("SAPI.SpVoice")
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject('SAPI.SpFileStream')
infile = r'D:\testdata\Voices_text.txt'
outfile = 'D:/testdata/'
f = open(infile, 'r', encoding='utf-8')
while True:
LineData = f.readline() # 按行读取数据
if not LineData:
break
# 截取LineData首部第1-4个字符作为编号
stnum = LineData[:6]
stream.Open(outfile + stnum + ".wav", SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
# 截取LineData第4个至尾部的字符作为语音内容
mytxt = LineData[4:]
# speak.Speak(mytxt) # 朗读
engine.speak(mytxt)
stream.close()
f.close() # 关闭文本文件
结果