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 socket 超时设置 errno 10054
Jul 01 Python
Python3读取zip文件信息的方法
May 22 Python
浅谈Python类的__getitem__和__setitem__特殊方法
Dec 25 Python
python处理Excel xlrd的简单使用
Sep 12 Python
Python编程实现使用线性回归预测数据
Dec 07 Python
python 图像处理画一个正弦函数代码实例
Sep 10 Python
python编写计算器功能
Oct 25 Python
keras自动编码器实现系列之卷积自动编码器操作
Jul 03 Python
详解pycharm自动import所需的库的操作方法
Nov 30 Python
python从ftp获取文件并下载到本地
Dec 05 Python
selenium自动化测试入门实战
Dec 21 Python
python plt.plot bar 如何设置绘图尺寸大小
Jun 01 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版本号
2006/10/09 PHP
global.php
2006/12/09 PHP
php学习笔记(三)操作符与控制结构
2011/08/06 PHP
Yii分页用法实例详解
2014/12/04 PHP
php实现的一个简单json rpc框架实例
2015/03/30 PHP
Symfony2学习笔记之控制器用法详解
2016/03/17 PHP
PHP获取指定日期是星期几的实现方法
2016/11/30 PHP
PHP面相对象中的重载与重写
2017/02/13 PHP
php7 图形用户界面GUI 开发示例
2020/02/22 PHP
Js 本页面传值实现代码
2009/05/17 Javascript
jQuery之日期选择器的深入解析
2013/06/19 Javascript
jquery操作angularjs对象
2015/06/26 Javascript
React-Native 组件之 Modal的使用详解
2017/08/08 Javascript
vue.js过滤器+ajax实现事件监听及后台php数据交互实例
2018/05/22 Javascript
element vue Array数组和Map对象的添加与删除操作
2018/11/14 Javascript
微信小程序学习笔记之目录结构、基本配置图文详解
2019/03/28 Javascript
Js实现复选框的全选、全不选反选功能代码实例
2020/02/28 Javascript
[53:10]Secret vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[01:07:46]完美世界DOTA2联赛循环赛 Magma vs IO BO2第二场 11.01
2020/11/02 DOTA
python多重继承新算法C3介绍
2014/09/28 Python
详解python中的json的基本使用方法
2016/12/21 Python
python 3.5下xadmin的使用及修复源码bug
2017/05/10 Python
Python2.7下安装Scrapy框架步骤教程
2017/12/22 Python
python通过tcp发送xml报文的方法
2018/12/28 Python
python实现图片横向和纵向拼接
2020/03/05 Python
python调用私有属性的方法总结
2020/07/24 Python
python实现图像随机裁剪的示例代码
2020/12/10 Python
Sunglasses Shop荷兰站:英国最大的太阳镜独立在线零售商和供应商
2017/01/08 全球购物
Jack Rogers官网:美国经典的女性鞋靴品牌
2019/09/04 全球购物
办公室主任竞聘演讲稿
2014/05/15 职场文书
会计师事务所实习证明
2014/11/16 职场文书
事业单位个人总结
2015/02/12 职场文书
2015年预算员工作总结
2015/05/14 职场文书
2016抗战胜利71周年红领巾广播稿
2015/12/18 职场文书
Python办公自动化之Excel(中)
2021/05/24 Python
Python 中random 库的详细使用
2021/06/03 Python