文字转语音

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

复制代码
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()
相关推荐
飞Link5 小时前
什么是 AirSim?一文读懂无人机仿真平台
python·算法·无人机
曲径海盗5 小时前
【Python 学习第 38 天】for 循环
python·学习笔记·编程入门
在世修行6 小时前
智学文枢:变式题生成引擎
python·ai·pyside·题目生成
镭封7 小时前
从视频文案提取到 AI 配音,短视频制作需要哪些工具?
人工智能·智能手机·音视频·语音识别·媒体
On The Road :7 小时前
Java 多线程详解:从入门到线程安全
java·python·安全
岁月宁静8 小时前
如何设计支持 Skills 的 Agent?一套可扩展的四层架构
python·agent
岁月宁静8 小时前
CrewAI 快速上手:10 分钟搭建你的第一个 Agent 团队
python·agent
2601_955662468 小时前
搞笑视频配音怎么做?如何选择有辨识度的声音
大数据·人工智能·音视频·语音识别·媒体
曲径海盗8 小时前
【Python 学习第 40 天】for 循环
python·学习笔记·编程入门
专注仿真8 小时前
知识问答大模型-rag算法详解
人工智能·python·控制算法