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 相关文章推荐
Django中实现一个高性能计数器(Counter)实例
Jul 09 Python
Python文件夹与文件的相关操作(推荐)
Jul 25 Python
Python 列表(List) 的三种遍历方法实例 详解
Apr 15 Python
Python切片操作实例分析
Mar 16 Python
pandas分区间,算频率的实例
Jul 04 Python
Python爬虫实现使用beautifulSoup4爬取名言网功能案例
Sep 15 Python
利用pandas合并多个excel的方法示例
Oct 10 Python
解决Django Haystack全文检索为空的问题
May 19 Python
keras的ImageDataGenerator和flow()的用法说明
Jul 03 Python
详解python内置模块urllib
Sep 09 Python
Python为何不支持switch语句原理详解
Oct 21 Python
python基于爬虫+django,打造个性化API接口
Jan 21 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重新实现PHP脚本引擎内置函数
2007/03/06 PHP
php mailer类调用远程SMTP服务器发送邮件实现方法
2016/03/04 PHP
PHP反射学习入门示例
2019/06/14 PHP
基于laravel-admin 后台 列表标签背景的使用方法
2019/10/03 PHP
JavaScript中的Screen屏幕对象
2008/01/16 Javascript
jQuery maxlength文本字数限制插件
2010/04/16 Javascript
js 浏览本地文件夹系统示例代码
2013/10/24 Javascript
jQuery匹配文档链接并添加class的方法
2015/06/26 Javascript
JavaScript实现图片轮播的方法
2015/07/31 Javascript
jQuery实现Email邮箱地址自动补全功能代码
2015/11/03 Javascript
javascript:void(0)是什么意思及href=#与href=javascriptvoid(0)的区别
2015/11/13 Javascript
requireJS使用指南
2016/04/27 Javascript
js仿手机页面文件下拉刷新效果
2016/10/14 Javascript
Angular 4 指令快速入门教程
2017/06/07 Javascript
Vue-路由导航菜单栏的高亮设置方法
2018/03/17 Javascript
vue项目设置scrollTop不起作用(总结)
2018/12/21 Javascript
vue里如何主动销毁keep-alive缓存的组件
2019/03/21 Javascript
Vue 引入AMap高德地图的实现代码
2019/04/29 Javascript
javascript+Canvas实现画板功能
2020/06/23 Javascript
vue中如何自定义右键菜单详解
2020/12/08 Vue.js
python3.6利用pyinstall打包py为exe的操作实例
2018/10/31 Python
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
详解python中的三种命令行模块(sys.argv,argparse,click)
2020/12/15 Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
2021/01/29 Python
CSS3 旋转立方体问题详解
2020/01/09 HTML / CSS
使用纯HTML5编写一款网页上的时钟的代码分享
2015/11/16 HTML / CSS
马来西亚网上购物:Youbeli
2018/03/30 全球购物
酒吧总经理岗位职责
2013/12/10 职场文书
英语专业个人求职信范文
2014/02/01 职场文书
导购员的岗位职责
2014/02/08 职场文书
商业房地产广告语
2014/03/13 职场文书
校运会口号
2014/06/18 职场文书
高中生旷课检讨书
2014/10/08 职场文书
初三毕业评语
2014/12/26 职场文书
环保建议书作文400字
2015/09/14 职场文书
励志语录:你若不勇敢,谁替你坚强
2019/11/08 职场文书