python实现将文本转换成语音的方法


Posted in Python onMay 28, 2015

本文实例讲述了python将文本转换成语音的方法。分享给大家供大家参考。具体实现方法如下:

# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente
# download installer file pyTTS-3.0.win32-py2.4.exe 
# from: http://sourceforge.net/projects/uncassist
# also needs: http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi
# and pywin32-204.win32-py2.4.exe at this date the latest version of win32com
# from: http://sourceforge.net/projects/pywin32/
# tested with Python24 on a Windows XP computer  vagaseat  15jun2005
import pyTTS
import time
tts = pyTTS.Create()
# set the speech rate, higher value = faster
# just for fun try values of -10 to 10
tts.Rate = 1
print "Speech rate =", tts.Rate
# set the speech volume percentage (0-100%)
tts.Volume = 90
print "Speech volume =", tts.Volume
# get a list of all the available voices
print "List of voices =", tts.GetVoiceNames()
# explicitly set a voice
tts.SetVoiceByName('MSMary')
print "Voice is set ot MSMary"
print
# announce the date and time, does a good job
timeStr = "The date and time is " + time.asctime()
print timeStr
tts.Speak(timeStr)
print
str1 = """
A young executive was leaving the office at 6 pm when he found 
the CEO standing in front of a shredder with a piece of paper in hand. 
"Listen," said the CEO, "this is important, and my secretary has left. 
Can you make this thing work?"
"Certainly," said the young executive. He turned the machine on, 
inserted the paper, and pressed the start button.
"Excellent, excellent!" said the CEO as his paper disappeared inside 
the machine. "I just need one copy."
"""
print str1
tts.Speak(str1)
tts.Speak('Haah haa haah haa')
print
str2 = """
Finagle's fourth law:
 Once a job is fouled up, anything done to improve it only makes it worse.
"""
print str2
print
print "The spoken text above has been written to a wave file (.wav)"
tts.SpeakToWave('Finagle4.wav', str2)
print "The wave file is loaded back and spoken ..."
tts.SpeakFromWave('Finagle4.wav')
print
print "Substitute a hard to pronounce word like Ctrl key ..."
#create an instance of the pronunciation corrector
p = pyTTS.Pronounce()
# replace words that are hard to pronounce with something that 
# is spelled out or misspelled, but at least sounds like it
p.AddMisspelled('Ctrl', 'Control')
str3 = p.Correct('Please press the Ctrl key!')
tts.Speak(str3)
print
print "2 * 3 = 6"
tts.Speak('2 * 3 = 6')
print
tts.Speak("sounds goofy, let's replace * with times")
print "Substitute * with times"
# ' * ' needs the spaces
p.AddMisspelled(' * ', 'times')
str4 = p.Correct('2 * 3 = 6')
tts.Speak(str4)
print
print "Say that real fast a few times!"
str5 = "The sinking steamer sunk!"
tts.Rate = 3
for k in range(7):
  print str5
  tts.Speak(str5)
  time.sleep(0.3)
tts.Rate = 0
tts.Speak("Wow, not one mispronounced word!")

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python实现进程同步和通信的方法
Jan 02 Python
wxPython之解决闪烁的问题
Jan 15 Python
详解Python 装饰器执行顺序迷思
Aug 08 Python
python实现电子书翻页小程序
Jul 23 Python
Python 获取指定文件夹下的目录和文件的实现
Aug 30 Python
win10下安装Anaconda的教程(python环境+jupyter_notebook)
Oct 23 Python
详解Django配置优化方法
Nov 18 Python
基于Python执行dos命令并获取输出的结果
Dec 30 Python
pytorch 常用线性函数详解
Jan 15 Python
Python 生成VOC格式的标签实例
Mar 10 Python
python 爬取小说并下载的示例
Dec 07 Python
python装饰器代码深入讲解
Mar 01 Python
Python 26进制计算实现方法
May 28 #Python
Python中super关键字用法实例分析
May 28 #Python
Python使用Supervisor来管理进程的方法
May 28 #Python
Python运算符重载用法实例
May 28 #Python
Python smallseg分词用法实例分析
May 28 #Python
Python基于smtplib实现异步发送邮件服务
May 28 #Python
Python使用Scrapy爬取妹子图
May 28 #Python
You might like
用文本文件制作留言板提示(上)
2006/10/09 PHP
基于PHP实现商品成交时发送短信功能
2016/05/11 PHP
Gambit vs ForZe BO3 第一场 2.13
2021/03/10 DOTA
Javascript条件判断使用小技巧总结
2008/09/08 Javascript
JavaScript之自定义类型
2012/05/04 Javascript
JavaScript prototype属性深入介绍
2012/11/27 Javascript
jquery Mobile入门—多页面切换示例学习
2013/01/08 Javascript
jquery创建一个ajax关键词数据搜索实现思路
2013/02/26 Javascript
jQuery实现的导航动画效果(附demo源码)
2016/04/01 Javascript
解决同一页面中两个iframe互相调用jquery,js函数的方法
2016/12/12 Javascript
nodejs模块学习之connect解析
2017/07/05 NodeJs
使用mock.js随机数据和使用express输出json接口的实现方法
2018/01/07 Javascript
微信小程序时间标签和时间范围的联动效果
2019/02/15 Javascript
使用vue-cli3新建一个项目并写好基本配置(推荐)
2019/04/24 Javascript
JavaScript 继承 封装 多态实现及原理详解
2019/07/29 Javascript
vue实现标签云效果的示例
2020/11/09 Javascript
Python中的zip函数使用示例
2015/01/29 Python
python修改操作系统时间的方法
2015/05/18 Python
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
解决python os.mkdir创建目录失败的问题
2018/10/16 Python
Python sklearn KFold 生成交叉验证数据集的方法
2018/12/11 Python
浅谈django2.0 ForeignKey参数的变化
2019/08/06 Python
如何使用Python破解ZIP或RAR压缩文件密码
2020/01/09 Python
python如何求数组连续最大和的示例代码
2020/02/04 Python
python 多线程中join()的作用
2020/10/29 Python
Python Serial串口基本操作(收发数据)
2020/11/06 Python
波兰在线体育用品商店:Hop-Sport.pl
2019/07/23 全球购物
学期自我鉴定
2013/11/04 职场文书
学校党的群众路线教育实践活动总结报告
2014/07/03 职场文书
总经理助理岗位职责范本
2014/07/20 职场文书
社区党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
银行领导班子四风对照检查材料
2014/09/27 职场文书
任长霞观后感
2015/06/16 职场文书
公文格式,规则明细(新手收藏)
2019/07/23 职场文书
JS开发前端团队展示控制器来为成员引流
2022/08/14 Javascript
Win10服务主机占用内存怎么办?Win10服务主机进程占用大量内存解决方法
2022/09/23 数码科技