文字转语音

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

复制代码
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()
相关推荐
小白学大数据2 分钟前
实时监控 1688 商品价格变化的爬虫系统实现
javascript·爬虫·python
Darkershadow7 分钟前
Python学习之使用笔记本摄像头截屏
python·opencv·学习
ekprada10 分钟前
Day 40 深度学习训练与测试的规范写法
人工智能·python
音视频牛哥14 分钟前
C#实战:如何开发设计毫秒级延迟、工业级稳定的Windows平台RTSP/RTMP播放器
人工智能·机器学习·机器人·c#·音视频·rtsp播放器·rtmp播放器
Blossom.1181 小时前
基于时序大模型+强化学习的虚拟电厂储能调度系统:从负荷预测到收益最大化的实战闭环
运维·人工智能·python·决策树·机器学习·自动化·音视频
深蓝海拓2 小时前
PySide6从0开始学习的笔记(四)QMainWindow
笔记·python·学习·pyqt
深蓝海拓2 小时前
PySide6 的 QSettings简单应用学习笔记
python·学习·pyqt
码界奇点9 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
Laravel技术社区10 小时前
pytesseract 中英文 识别图片文字
python
JS-s11 小时前
【无标题】
音视频