文字转语音

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

复制代码
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 分钟前
python --货车装厢问题
开发语言·python
Y1nhl12 分钟前
力扣_链表_python版本
开发语言·python·算法·leetcode·链表·职场和发展
qianbo_insist1 小时前
c++ python 共享内存
开发语言·c++·python
凌览2 小时前
有了 25k Star 的MediaCrawler爬虫库加持,三分钟搞定某红书、某音等平台爬取!
前端·后端·python
这里有鱼汤2 小时前
给你的DeepSeek装上实时行情,让他帮你炒股
后端·python·mcp
Tomorrow'sThinker2 小时前
[特殊字符] Excel 读取收件人 + Outlook 批量发送带附件邮件 —— Python 自动化实战
python·excel·outlook
JosieBook3 小时前
【Java编程动手学】Java常用工具类
java·python·mysql
灵智工坊LingzhiAI5 小时前
人体坐姿检测系统项目教程(YOLO11+PyTorch+可视化)
人工智能·pytorch·python
karmueo465 小时前
视频序列和射频信号多模态融合算法Fusion-Vital解读
算法·音视频·多模态
昨日之日20065 小时前
Video Background Remover V3版 - AI视频一键抠像/视频换背景 支持50系显卡 一键整合包下载
人工智能·音视频