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中使用logging模块打印log日志详解
Apr 05 Python
Python实现求笛卡尔乘积的方法
Sep 16 Python
Python+request+unittest实现接口测试框架集成实例
Mar 16 Python
Python实现截取PDF文件中的几页代码实例
Mar 11 Python
python处理excel绘制雷达图
Oct 18 Python
Python打开文件、文件读写操作、with方式、文件常用函数实例分析
Jan 07 Python
PyQt5实现简单的计算器
May 30 Python
python tqdm实现进度条的示例代码
Nov 10 Python
用Python制作灯光秀短视频的思路详解
Apr 13 Python
解决Django transaction进行事务管理踩过的坑
Apr 24 Python
python办公自动化之excel的操作
May 23 Python
python3中apply函数和lambda函数的使用详解
Feb 28 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使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
php表单加入Token防止重复提交的方法分析
2016/10/10 PHP
PHP微信企业号开发之回调模式开启与用法示例
2017/11/25 PHP
PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例
2018/06/09 PHP
完美兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面js代码
2014/12/17 Javascript
JQuery zClip插件实现复制页面内容到剪贴板
2015/11/02 Javascript
Vue.js组件tabs实现选项卡切换效果
2016/12/01 Javascript
教你一步步用jQyery实现轮播器
2016/12/18 Javascript
nodejs入门教程一:概念与用法简介
2017/04/24 NodeJs
jquery.uploadView 实现图片预览上传功能
2017/08/10 jQuery
vue debug 二种方法
2018/09/16 Javascript
微信小程序实现简单表格
2019/02/14 Javascript
vue表单数据交互提交演示教程
2019/11/13 Javascript
[01:59]翻天覆地,因你而变,7.20版本地图更新速览
2018/11/24 DOTA
python实现图片变亮或者变暗的方法
2015/06/01 Python
在DigitalOcean的服务器上部署flaskblog应用
2015/12/19 Python
Python 的描述符 descriptor详解
2016/02/27 Python
python实现给微信公众号发送消息的方法
2017/06/30 Python
python图像常规操作
2017/11/11 Python
Python文本特征抽取与向量化算法学习
2017/12/22 Python
python 字典操作提取key,value的方法
2019/06/26 Python
pytorch的batch normalize使用详解
2020/01/15 Python
Python中url标签使用知识点总结
2020/01/16 Python
python matplotlib imshow热图坐标替换/映射实例
2020/03/14 Python
使用Python下载抖音各大V视频的思路详解
2021/02/06 Python
基于CSS3实现立方体自转效果
2016/03/01 HTML / CSS
phpquery中文手册
2021/03/18 PHP
英国户外装备商店:Ultimate Outdoors
2019/05/07 全球购物
什么是GWT的Entry Point
2013/08/16 面试题
2015年度培训工作总结范文
2015/04/02 职场文书
2015年公司工作总结
2015/04/25 职场文书
技能培训通讯稿
2015/07/18 职场文书
2016年“12.3”国际残疾人日活动总结
2016/04/01 职场文书
在 HTML 页面中使用 React的场景分析
2022/01/18 Javascript
HTML CSS 一个标签实现带动画的抖音LOGO
2022/04/26 HTML / CSS
详解Mysql事务并发(脏读、不可重复读、幻读)
2022/04/29 MySQL