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装饰器decorator介绍
Nov 21 Python
在Python程序员面试中被问的最多的10道题
Dec 05 Python
书单|人生苦短,你还不用python!
Dec 29 Python
详谈python中冒号与逗号的区别
Apr 18 Python
基于Python pip用国内镜像下载的方法
Jun 12 Python
python 构造三维全零数组的方法
Nov 12 Python
python学生管理系统
Jan 30 Python
对pandas处理json数据的方法详解
Feb 08 Python
python实现ip代理池功能示例
Jul 05 Python
Django 模型类(models.py)的定义详解
Jul 19 Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 Python
Python3爬虫里关于Splash负载均衡配置详解
Jul 10 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函数http_build_query使用详解
2014/08/20 PHP
PHP面向对象自动加载机制原理与用法分析
2016/10/14 PHP
laravel excel 上传文件保存到本地服务器功能
2019/11/14 PHP
理解JavaScript变量作用域更轻松
2009/10/25 Javascript
分享一个自己写的table表格排序js插件(高效简洁)
2011/10/29 Javascript
javascript屏蔽右键代码
2014/05/15 Javascript
JS常用表单验证方法总结
2014/05/22 Javascript
JavaScript严格模式禁用With语句的原因
2014/10/20 Javascript
js中数组排序sort方法的原理分析
2014/11/20 Javascript
JavaScript关于提高网站性能的几点建议(一)
2016/07/24 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
JQueryMiniUI按照时间进行查询的实现方法
2017/06/07 jQuery
利用CDN加速react webpack打包后的文件详解
2018/02/22 Javascript
一个简单的node.js界面实现方法
2018/06/01 Javascript
React+TypeScript+webpack4多入口配置详解
2019/08/08 Javascript
ES6新增的数组知识实例小结
2020/05/23 Javascript
详解element-ui动态限定的日期范围选择器代码片段
2020/07/03 Javascript
Vue实现Header渐隐渐现效果的实例代码
2020/11/05 Javascript
[56:35]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第一局
2016/03/06 DOTA
python下函数参数的传递(参数带星号的说明)
2010/09/19 Python
Python version 2.7 required, which was not found in the registry
2014/08/26 Python
linux查找当前python解释器的位置方法
2019/02/20 Python
python实现字典嵌套列表取值
2019/12/16 Python
Python利用Faiss库实现ANN近邻搜索的方法详解
2020/08/03 Python
Python爬虫爬取微信朋友圈
2020/08/06 Python
让IE支持HTML5的方法
2012/12/11 HTML / CSS
详解HTML5常用的语义化标签
2019/09/27 HTML / CSS
教师党员公开承诺书
2014/03/25 职场文书
3的组成教学反思
2014/04/30 职场文书
森林防火标语
2014/06/23 职场文书
迎新春趣味活动方案
2014/08/24 职场文书
报到证办理个人委托书
2014/10/06 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
公务员年度考核评语
2014/12/31 职场文书
军训后的感想
2015/08/07 职场文书
暑假开始了,你的暑假学习计划写好了吗?
2019/07/04 职场文书