键盘获取文字,转化为语音后保存本地
from win32com.client import Dispatch
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
speaker=Dispatch('SAPI.SpVoice')
speaker.Speak('请输入你想转化的文字')
data=input('请输入:')#speaker.Speak(data)
engine=CreateObject("SAPI.SpVoice")
stream=CreateObject('SAPI.SpFileStream')# 保存写入文字
infile='语音文字.txt'
with open(infile, 'w', encoding='utf-8') as fp:fp.write(data)# 保存音频
outfile='语音2.wav'
stream.Open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream=streamf=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()
engine.speak(theText)stream.close()
speaker.Speak('您输入的语音已保存!')
print('您输入的语音已保存!')del speaker
读取txt文件中的文字,朗读并存储音频
from comtypes.client import CreateObject
from comtypes.gen import SpeechLibengine=CreateObject("SAPI.SpVoice")
stream=CreateObject('SAPI.SpFileStream')# 打开txt
infile='./text/word.txt'
f=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()# speaker.Speak(theText) # 朗读文字# 文字转语音并存储
outfile='语音.wav'
stream.Open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream=streamengine.speak(theText)
stream.close()