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编写Linux系统守护进程实例
Feb 03 Python
python中map、any、all函数用法分析
Apr 21 Python
十条建议帮你提高Python编程效率
Feb 16 Python
Python中元组,列表,字典的区别
May 21 Python
python发送邮件实例分享
Jul 28 Python
对Python中数组的几种使用方法总结
Jun 28 Python
pyqt5的QWebEngineView 使用模板的方法
Aug 18 Python
解决pip install xxx报错SyntaxError: invalid syntax的问题
Nov 30 Python
python topk()函数求最大和最小值实例
Apr 02 Python
Python操作PostgreSql数据库的方法(基本的增删改查)
Dec 29 Python
Django开发RESTful API实现增删改查(入门级)
May 10 Python
Python机器学习之KNN近邻算法
May 14 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
php下检测字符串是否是utf8编码的代码
2008/06/28 PHP
php后退一页表单内容保存实现方法
2012/06/17 PHP
php float不四舍五入截取浮点型字符串方法总结
2013/10/28 PHP
phpstudy默认不支持64位php的解决方法
2017/02/20 PHP
PHP生成(支持多模板)二维码海报代码
2018/04/30 PHP
PHP自定义错误处理的方法分析
2018/12/19 PHP
一步一步制作jquery插件Tabs实现过程
2010/07/06 Javascript
JQuery动态给table添加、删除行 改进版
2011/01/19 Javascript
JS截取字符串常用方法整理及使用示例
2013/10/18 Javascript
js实现选中页面文字将其分享到新浪微博
2015/11/05 Javascript
js实现淡入淡出轮播切换功能
2017/01/13 Javascript
微信小程序 数据绑定及运算的简单实例
2017/09/20 Javascript
原生JS实现前端本地文件上传
2018/09/08 Javascript
解决node-sass偶尔安装失败的方法小结
2018/12/05 Javascript
Python的词法分析与语法分析
2013/05/18 Python
Python程序员开发中常犯的10个错误
2014/07/07 Python
浅析Python中的多进程与多线程的使用
2015/04/07 Python
Python基于sftp及rsa密匙实现远程拷贝文件的方法
2016/09/21 Python
Python整型运算之布尔型、标准整型、长整型操作示例
2017/07/21 Python
Python使用一行代码获取上个月是几月
2018/08/30 Python
Python实现带参数的用户验证功能装饰器示例
2018/12/14 Python
python 提取tuple类型值中json格式的key值方法
2018/12/31 Python
Python给图像添加噪声具体操作
2019/03/03 Python
Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
2019/05/27 Python
Python socket 套接字实现通信详解
2019/08/27 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
爱尔兰家电数码商城:Currys PC World爱尔兰
2016/07/23 全球购物
婚礼证婚人证婚词
2014/01/13 职场文书
实验教师岗位职责
2014/02/13 职场文书
广告词串烧
2014/03/19 职场文书
幼儿园中班开学寄语
2014/04/03 职场文书
工商管理专业毕业生自我鉴定2014
2014/10/04 职场文书
写给老师的感谢信
2015/01/20 职场文书
2016年教师党员创先争优承诺书
2016/03/24 职场文书
mysql自增长id用完了该怎么办
2022/02/12 MySQL
i7 6700处理器相当于i5几代
2022/04/19 数码科技