文字转语音

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

复制代码
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()
相关推荐
可触的未来,发芽的智生1 小时前
触摸未来2025.10.10:记忆的种子,当神经网络拥有了临时工作区,小名喜忆记系统
人工智能·python·神经网络·机器学习·架构
mortimer1 小时前
在 Windows 上部署 NVIDIA Parakeet-TDT 遇到的坑
python·github·nvidia
Rock_yzh2 小时前
AI学习日记——卷积神经网络(CNN):完整实现与可视化分析
人工智能·python·深度学习·神经网络·学习·cnn
生信小白菜儿2 小时前
深度学习(DL)概念及实例操作
人工智能·python·深度学习
测试老哥2 小时前
如何编写好测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
郝学胜-神的一滴3 小时前
Effective Python 第44条:用纯属性与修饰器取代旧式的 setter 与 getter 方法
开发语言·python·程序人生·软件工程
嫂子的姐夫4 小时前
11-py调用js
javascript·爬虫·python·网络爬虫·爬山算法
hazy1k4 小时前
K230基础-录放视频
网络·人工智能·stm32·单片机·嵌入式硬件·音视频·k230
图亚Vanta5 小时前
Python入门第一课:Python安装、VSCode/Pycharm配置
vscode·python·pycharm
睿思达DBA_WGX5 小时前
使用 python-docx 库操作 word 文档(2):在word文档中插入各种内容
python·word