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实现对比不同字体中的同一字符的显示效果
Apr 23 Python
python同时给两个收件人发送邮件的方法
Apr 30 Python
Python写入数据到MP3文件中的方法
Jul 10 Python
python3正则提取字符串里的中文实例
Jan 31 Python
python调用外部程序的实操步骤
Mar 04 Python
使用Python实现画一个中国地图
Nov 23 Python
Python实现中值滤波去噪方式
Dec 18 Python
浅谈python3打包与拆包在函数的应用详解
May 02 Python
Python实现加密接口测试方法步骤详解
Jun 05 Python
Django 用户认证Auth组件的使用
Nov 30 Python
pytorch中Schedule与warmup_steps的用法说明
May 24 Python
OpenCV-Python实现图像平滑处理操作
Jun 08 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 和 XML: 使用expat函数(三)
2006/10/09 PHP
php中用foreach来操作数组的代码
2011/07/17 PHP
国外十大最流行的PHP框架排名
2013/07/04 PHP
php获取bing每日壁纸示例分享
2014/02/25 PHP
PHP单例模式简单用法示例
2017/06/23 PHP
capacityFixed 基于jquery的类似于新浪微博新消息提示的定位框
2011/05/24 Javascript
JS简单实现元素复制示例附图
2013/11/19 Javascript
angular中使用路由和$location切换视图
2015/01/23 Javascript
javascript实现设置、获取和删除Cookie的方法
2015/06/01 Javascript
js实现小窗口拖拽效果
2016/12/03 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
详解vue与后端数据交互(ajax):vue-resource
2017/03/16 Javascript
jQuery Validate格式验证功能实例代码(包括重名验证)
2017/07/18 jQuery
javascript函数的节流[throttle]与防抖[debounce]
2017/11/15 Javascript
Vue.js添加组件操作示例
2018/06/13 Javascript
探索JavaScript中私有成员的相关知识
2019/06/13 Javascript
[01:18]DOTA2超级联赛专访hanci ForLove淘汰感言曝光
2013/06/04 DOTA
python k-近邻算法实例分享
2014/06/11 Python
Python中使用SAX解析xml实例
2014/11/21 Python
在Python的Flask框架下收发电子邮件的教程
2015/04/21 Python
TensorFlow 滑动平均的示例代码
2018/06/19 Python
Python使用线程来接收串口数据的示例
2019/07/02 Python
Python3+PyInstall+Sciter解决报错缺少dll、html等文件问题
2019/07/15 Python
Django认证系统实现的web页面实现代码
2019/08/12 Python
Python request中文乱码问题解决方案
2020/09/17 Python
Ray-Ban雷朋美国官网:全球领先的太阳眼镜品牌
2016/07/20 全球购物
Booking.com美国:全球酒店预订网站
2017/04/18 全球购物
美国首屈一指的高品质珠宝设计师和零售商:Allurez
2018/01/23 全球购物
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
德国珠宝和配件商店:Styleserver
2021/02/23 全球购物
事业单位请假制度
2014/01/13 职场文书
大队委竞选演讲稿
2014/04/28 职场文书
研发工程师岗位职责
2014/04/28 职场文书
2015年财务人员工作总结
2015/04/10 职场文书
浅谈Python响应式类库RxPy
2021/06/14 Python
15个值得收藏的JavaScript函数
2021/09/15 Javascript