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协议的一个简单WebSocket服务器
Jun 04 Python
python 字典(dict)遍历的四种方法性能测试报告
Jun 25 Python
Python使用sftp实现上传和下载功能(实例代码)
Mar 14 Python
一个基于flask的web应用诞生 组织结构调整(7)
Apr 11 Python
python+selenium实现京东自动登录及秒杀功能
Nov 18 Python
PyQt5每天必学之日历控件QCalendarWidget
Apr 19 Python
Windows下python3.7安装教程
Jul 31 Python
很酷的python表白工具 你喜欢我吗
Apr 11 Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
Jun 25 Python
Python-copy()与deepcopy()区别详解
Jul 12 Python
Python符号计算之实现函数极限的方法
Jul 15 Python
基于pytorch padding=SAME的解决方式
Feb 18 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 遍历显示文件夹下所有目录、所有文件的函数,没有分页的代码
2008/11/14 PHP
php实现从ftp服务器上下载文件树到本地电脑的程序
2009/02/10 PHP
检测png图片是否完整的php代码
2010/09/06 PHP
php处理带有中文URL的方法
2016/07/11 PHP
Laravel5.1 框架Middleware中间件基本用法实例分析
2020/01/04 PHP
javascript TextArea动态显示剩余字符
2008/10/22 Javascript
js中reverse函数的用法详解
2013/12/26 Javascript
nodejs教程之异步I/O
2014/11/21 NodeJs
JS实现超过长度限制后自动跳转下一款文本框的方法
2015/02/23 Javascript
使用开源工具制作网页验证码的方法
2016/10/17 Javascript
javascript基本数据类型和转换
2017/03/17 Javascript
基于AGS JS开发自定义贴图图层
2017/03/31 Javascript
详解使用vue脚手架工具搭建vue-webpack项目
2017/05/10 Javascript
vue踩坑记录之数组定义和赋值问题
2019/03/20 Javascript
微信小程序如何播放腾讯视频的实现
2019/09/20 Javascript
vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例
2019/11/13 Javascript
vue实现登录功能
2020/12/31 Vue.js
[55:39]DOTA2-DPC中国联赛 正赛 VG vs LBZS BO3 第二场 1月19日
2021/03/11 DOTA
[01:00:49]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第二场 1月31日
2021/03/11 DOTA
详解python上传文件和字符到PHP服务器
2017/11/24 Python
Python一行代码实现快速排序的方法
2019/04/30 Python
Python中正反斜杠(‘/’和‘\’)的意义与用法
2019/08/12 Python
Python 取numpy数组的某几行某几列方法
2019/10/24 Python
使用python matplotlib 画图导入到word中如何保证分辨率
2020/04/16 Python
Python读入mnist二进制图像文件并显示实例
2020/04/24 Python
django 模型字段设置默认值代码
2020/07/15 Python
移动端Html5页面生成图片解决方案
2018/08/07 HTML / CSS
BrandAlley英国:法国折扣奢侈品网上零售商
2017/07/03 全球购物
阿迪达斯新加坡官方网站:adidas新加坡
2019/12/06 全球购物
给交警的表扬信
2014/01/12 职场文书
抄作业检讨书
2014/02/17 职场文书
利群广告词
2014/03/20 职场文书
法学专业大学生实习自我鉴定
2014/10/05 职场文书
Mysql外键约束的创建与删除的使用
2022/03/03 MySQL
Android开发之WECHAT微信小程序路由跳转的两种形式
2022/04/12 Java/Android
怎么禁用Win11输入法 最新Win11输入法关闭教程
2022/08/05 数码科技