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读写excel的方法
Nov 18 Python
Python简单计算文件MD5值的方法示例
Apr 11 Python
python计算两个地址之间的距离方法
Jun 09 Python
python实战教程之自动扫雷
Jul 13 Python
在python 不同时区之间的差值与转换方法
Jan 14 Python
Python3 使用pillow库生成随机验证码
Aug 26 Python
将自己的数据集制作成TFRecord格式教程
Feb 17 Python
Tensorflow之梯度裁剪的实现示例
Mar 08 Python
Python装饰器实现方法及应用场景详解
Mar 26 Python
Python接口测试结果集实现封装比较
May 01 Python
Django中使用Json返回数据的实现方法
Jun 03 Python
用Python 爬取猫眼电影数据分析《无名之辈》
Jul 24 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中使用break跳出多重循环代码实例
2015/01/21 PHP
php实现微信扫码支付
2017/03/26 PHP
Laravel框架集合用法实例浅析
2020/05/14 PHP
js form action动态修改方法
2008/11/04 Javascript
jquery 层次选择器siblings与nextAll的区别介绍
2013/08/02 Javascript
js Date概念详细介绍
2013/11/22 Javascript
node.js入门教程迷你书、node.js入门web应用开发完全示例
2014/04/06 Javascript
js中通过父级进行查找定位元素
2014/06/15 Javascript
node.js中的path.join方法使用说明
2014/12/08 Javascript
IE下使用jQuery重置iframe地址时内存泄露问题解决办法
2015/02/05 Javascript
js实现最短的XML格式化工具实例
2015/03/12 Javascript
为什么JS中eval处理JSON数据要加括号
2015/04/13 Javascript
Node.js中的process.nextTick使用实例
2015/06/25 Javascript
关于安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法
2016/05/17 Javascript
使用BootStrap实现悬浮窗口的效果
2016/12/13 Javascript
用jQuery.ajaxSetup实现对请求和响应数据的过滤
2016/12/20 Javascript
js实现文本上下来回滚动
2017/02/03 Javascript
js控制按钮,防止频繁点击响应的实例
2017/02/15 Javascript
jQuery Ajax向服务端传递数组参数值的实例代码
2017/09/03 jQuery
Vuejs2 + Webpack框架里,模拟下载的实例讲解
2018/09/05 Javascript
JavaScript ECMA-262-3 深入解析(一):执行上下文实例分析
2020/04/25 Javascript
[37:45]2014 DOTA2国际邀请赛中国区预选赛5.21 DT VS Orenda
2014/05/22 DOTA
[01:59]深扒TI7聊天轮盘语音出处 1
2017/05/11 DOTA
[43:43]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第三场 11.29
2020/12/02 DOTA
Python操作sqlite3快速、安全插入数据(防注入)的实例
2014/04/26 Python
神经网络(BP)算法Python实现及应用
2018/04/16 Python
python 实现selenium断言和验证的方法
2019/02/13 Python
Django框架静态文件使用/中间件/禁用ip功能实例详解
2019/07/22 Python
pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
2019/08/17 Python
发现两个有趣的CSS3动画效果
2013/08/14 HTML / CSS
世界领先的在线地板和建筑材料批发商:BuildDirect
2017/02/26 全球购物
一年级学生期末评语
2014/04/21 职场文书
顶岗实习协议书
2015/01/29 职场文书
结婚当天新郎保证书
2015/05/08 职场文书
PostgreSQL自动更新时间戳实例代码
2021/11/27 PostgreSQL
对讲机的最大通讯距离是多少
2022/02/18 无线电