键盘获取文字,转化为语音后保存本地
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=stream
f=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()
engine.speak(theText)
stream.close()
speaker.Speak('您输入的语音已保存!')
print('您输入的语音已保存!')
del speaker
文章来源地址https://www.toymoban.com/news/detail-628010.html
读取txt文件中的文字,朗读并存储音频
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine=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=stream
engine.speak(theText)
stream.close()
文章来源:https://www.toymoban.com/news/detail-628010.html
到了这里,关于文字转语音的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!