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中类的继承代码实例
Oct 28 Python
简述Python2与Python3的不同点
Jan 21 Python
python如何将图片转换为字符图片
Aug 19 Python
在django admin中添加自定义视图的例子
Jul 26 Python
基于Python安装pyecharts所遇的问题及解决方法
Aug 12 Python
解决Tensorflow 使用时cpu编译不支持警告的问题
Feb 03 Python
在python中利用pycharm自定义代码块教程(三步搞定)
Apr 15 Python
python实现画图工具
Aug 27 Python
Python利用myqr库创建自己的二维码
Nov 24 Python
用Python实现一个打字速度测试工具来测试你的手速
May 28 Python
Python使用psutil库对系统数据进行采集监控的方法
Aug 23 Python
python读取mnist数据集方法案例详解
Sep 04 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错误类型及屏蔽方法
2017/05/27 PHP
地震发生中逃生十大法则
2008/05/12 Javascript
基于jQuery的JavaScript模版引擎JsRender使用指南
2014/12/29 Javascript
JavaScript中toString()方法的使用详解
2015/06/05 Javascript
去除字符串左右两边的空格(实现代码)
2016/05/12 Javascript
Node.js的文件权限及读写flag详解
2016/10/11 Javascript
js实现对table的增加行和删除行的操作方法
2016/10/13 Javascript
详解vue 实例方法和数据
2017/10/23 Javascript
vue自定义指令directive实例详解
2018/01/17 Javascript
vue计算属性get和set用法示例
2019/02/08 Javascript
微信小程序Echarts覆盖正常组件问题解决
2019/07/13 Javascript
vux-scroller实现移动端上拉加载功能过程解析
2019/10/08 Javascript
python求pi的方法
2014/10/08 Python
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
python Django框架实现自定义表单提交
2016/03/25 Python
python解决方案:WindowsError: [Error 2]
2016/08/28 Python
在Windows中设置Python环境变量的实例讲解
2018/04/28 Python
Python实现的直接插入排序算法示例
2018/04/29 Python
判断python字典中key是否存在的两种方法
2018/08/10 Python
使用pandas实现csv/excel sheet互相转换的方法
2018/12/10 Python
python命令行工具Click快速掌握
2019/07/04 Python
tensorboard实现同时显示训练曲线和测试曲线
2020/01/21 Python
python批量修改交换机密码的示例
2020/09/22 Python
Django使用django-simple-captcha做验证码的实现示例
2021/01/07 Python
python利用proxybroker构建爬虫免费IP代理池的实现
2021/02/21 Python
anaconda升级sklearn版本的实现方法
2021/02/22 Python
什么是抽象
2015/12/13 面试题
工商学院毕业生自荐信
2013/11/12 职场文书
药剂专业求职信
2014/06/20 职场文书
学校清明节活动总结
2014/07/04 职场文书
小学体育组工作总结
2015/08/13 职场文书
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
2021/04/06 Python
Python基础之条件语句详解
2021/06/16 Python
一文搞懂python异常处理、模块与包
2021/06/26 Python
使用canvas仿Echarts实现金字塔图的实例代码
2021/11/11 HTML / CSS
windows server2016安装oracle 11g的图文教程
2022/07/15 Servers