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程序设计入门(3)数组的使用
Jun 16 Python
详解Python的Django框架中的模版继承
Jul 16 Python
Windows和Linux下Python输出彩色文字的方法教程
May 02 Python
Python3 修改默认环境的方法
Feb 16 Python
对Python定时任务的启动和停止方法详解
Feb 19 Python
Django跨域资源共享问题(推荐)
Mar 09 Python
Pytorch通过保存为ONNX模型转TensorRT5的实现
May 25 Python
Python astype(np.float)函数使用方法解析
Jun 08 Python
浅谈盘点5种基于Python生成的个性化语音方法
Feb 05 Python
Python中全局变量和局部变量的理解与区别
Feb 07 Python
基于Python实现的购物商城管理系统
Apr 27 Python
七个Python必备的GUI库
Apr 27 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使之能同时支持GIF和JPEG
2006/10/09 PHP
如何使用脚本模仿登陆过程
2006/11/22 PHP
PHP重定向与伪静态区别
2017/02/19 PHP
PHP simplexml_import_dom()函数讲解
2019/02/03 PHP
jQuery1.6 正式版发布并提供下载
2011/05/05 Javascript
jQuery+JSON+jPlayer实现QQ空间音乐查询功能示例
2013/06/17 Javascript
Jquery动态改变图片IMG的src地址示例
2013/06/25 Javascript
网页中可关闭的漂浮窗口实现可自行调节
2013/08/20 Javascript
js实现向右横向滑出的二级菜单效果
2015/08/27 Javascript
详解JS中Array对象扩展与String对象扩展
2016/01/07 Javascript
动态创建按钮的JavaScript代码
2016/01/29 Javascript
html+javascript+bootstrap实现层级多选框全层全选和多选功能
2017/03/09 Javascript
20170918 前端开发周报之JS前端开发必看
2017/09/18 Javascript
jQueryMobile之窗体长内容的缺陷与解决方法实例分析
2017/09/20 jQuery
JavaScript设计模式之构造函数模式实例教程
2018/07/02 Javascript
微信小程序自定义toast弹窗效果的实现代码
2018/11/15 Javascript
js实现简单掷骰子小游戏
2019/10/24 Javascript
vue项目引入ts步骤(小结)
2019/10/31 Javascript
jQuery模仿ToDoList实现简单的待办事项列表
2019/12/30 jQuery
如何利用javascript接收json信息并进行处理
2020/08/06 Javascript
[01:55]2014DOTA2国际邀请赛快报:国土生病 紧急去医院治疗
2014/07/10 DOTA
python获取目录下所有文件的方法
2015/06/01 Python
python 写入csv乱码问题解决方法
2016/10/23 Python
Tensorflow简单验证码识别应用
2017/05/25 Python
python多线程同步之文件读写控制
2021/02/25 Python
Python 复平面绘图实例
2019/11/21 Python
Python完全识别验证码自动登录实例详解
2019/11/24 Python
Python搭建HTTP服务过程图解
2019/12/14 Python
Cpython解释器中的GIL全局解释器锁
2020/11/09 Python
Python调用ffmpeg开源视频处理库,批量处理视频
2020/11/16 Python
突袭HTML5之Javascript API扩展2—地理信息服务及地理位置API学习
2013/01/31 HTML / CSS
全球性的在线商店:Vogca
2019/05/10 全球购物
SAZAC的动物连体衣和动物睡衣:Kigurumi Shop
2020/03/14 全球购物
外语系毕业生自荐信范文
2013/12/16 职场文书
怎么写自荐书范文
2014/02/12 职场文书
党员评议表自我评价范文
2014/10/20 职场文书