文字转语音

键盘获取文字,转化为语音后保存本地

复制代码
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

读取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()
相关推荐
TF男孩31 分钟前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在5 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP8 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805113 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i13 小时前
python中类的基本结构、特殊属性于MRO理解
python
kaixin_啊啊13 小时前
突破限制:Melody远程音频管理新体验
音视频
liwulin050614 小时前
【ESP32-CAM】HELLO WORLD
python
Doris_202314 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_202314 小时前
Python 模式匹配match case
前端·后端·python
这里有鱼汤14 小时前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员