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利用elaphe制作二维条形码实现代码
May 25 Python
python选择排序算法的实现代码
Nov 21 Python
Python中使用OpenCV库来进行简单的气象学遥感影像计算
Feb 19 Python
特征脸(Eigenface)理论基础之PCA主成分分析法
Mar 13 Python
django和vue实现数据交互的方法
Aug 21 Python
给大家整理了19个pythonic的编程习惯(小结)
Sep 25 Python
python 字典套字典或列表的示例
Dec 16 Python
浅谈Python访问MySQL的正确姿势
Jan 07 Python
Django中Q查询及Q()对象 F查询及F()对象用法
Jul 09 Python
python实现数学模型(插值、拟合和微分方程)
Nov 13 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
Feb 06 Python
Matlab求解数组中的最大值及它所在的具体位置
Apr 16 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错误信息方法的详解
2013/06/09 PHP
php自动加载autoload机制示例分享
2014/02/20 PHP
PHP实现适用于文件内容操作的分页类
2016/06/15 PHP
json 入门基础教程 推荐
2009/10/31 Javascript
jquery之empty()与remove()区别说明
2010/09/10 Javascript
javascript实现div浮动在网页最顶上并带关闭按钮效果实例
2013/08/13 Javascript
javascript中对Attr(dom中属性)的操作示例讲解
2013/12/02 Javascript
javascript修改表格背景色实例代码分享
2013/12/10 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
使用Sticker.js实现贴纸效果
2015/01/28 Javascript
jquery判断至少有一个checkbox被选中的方法
2015/06/05 Javascript
JavaScript中Function函数与Object对象的关系
2015/12/17 Javascript
Jquery元素追加和删除的实现方法
2016/05/24 Javascript
JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)
2016/06/02 Javascript
返回函数的JavaScript函数
2016/06/14 Javascript
Vue.js表单控件实践
2016/10/27 Javascript
JS实现的相册图片左右滚动完整实例
2016/11/23 Javascript
简单实现js菜单栏切换效果
2017/03/04 Javascript
JavaScript实现百度搜索框效果
2020/03/26 Javascript
详解Require.js与Sea.js的区别
2018/08/05 Javascript
vxe-table vue table 表格组件功能
2019/05/26 Javascript
Django中URLconf和include()的协同工作方法
2015/07/20 Python
Python循环语句中else的用法总结
2016/09/11 Python
在Python dataframe中出生日期转化为年龄的实现方法
2018/10/20 Python
在python 不同时区之间的差值与转换方法
2019/01/14 Python
pycharm运行scrapy过程图解
2019/11/22 Python
pytorch三层全连接层实现手写字母识别方式
2020/01/14 Python
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
2020/02/24 Python
html5画布旋转效果示例
2014/01/27 HTML / CSS
司机辞职报告范文
2014/01/20 职场文书
大学生实习证明范本
2014/09/19 职场文书
党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
党员领导干部民主生活会批评与自我批评发言
2014/09/28 职场文书
2015年学生资助工作总结
2015/05/25 职场文书
2015年学校综合治理工作总结
2015/07/20 职场文书
element tree树形组件回显数据问题解决
2022/08/14 Javascript