python实现将文本转换成语音的方法

 更新时间:2015年05月28日 12:46:38   作者:不吃皮蛋  
这篇文章主要介绍了python实现将文本转换成语音的方法,涉及Python中pyTTS模块的相关使用技巧,需要的朋友可以参考下

本文实例讲述了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中实现栈的三种方法

    这篇文章主要介绍了python中实现栈的三种方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • Ubuntu手动编译源码安装Python的详细过程

    Ubuntu手动编译源码安装Python的详细过程

    这篇文章主要介绍了Ubuntu手动编译源码安装Python的详细过程,在python官网找到所需版本的python安装包,下载到Ubuntu系统中,需要的朋友可以参考下
    2006-08-08
  • pandas如何将表中的字符串转成数值型

    pandas如何将表中的字符串转成数值型

    在用pd.read_csv读数据时,将要转换数据类型的列名和类型名构成字典,传给dtype,怎么转换呢,下面小编给大家分享下pandas将表中的字符串转成数值型,感兴趣的朋友一起看看吧
    2023-02-02
  • python读取图片任意范围区域

    python读取图片任意范围区域

    这篇文章主要为大家详细介绍了python读取图片任意范围区域,以一维数组形式返回,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Python的函数嵌套的使用方法

    Python的函数嵌套的使用方法

    这篇文章主要介绍了Python的函数嵌套的使用方法,有需要的朋友可以参考一下
    2014-01-01
  • python opencv读mp4视频的实例

    python opencv读mp4视频的实例

    今天小编就为大家分享一篇python opencv读mp4视频的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • 十一个案例带你吃透Python函数参数

    十一个案例带你吃透Python函数参数

    这篇文章主要通过十一个案例带大家一起了解一下Python中的函数参数,文中的示例代码讲解详细,对我们学习Python有一定帮助,需要的可以参考一下
    2022-08-08
  • python深度总结线性回归

    python深度总结线性回归

    这篇文章主要介绍了python的深度总结之线性回归,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • python实现下载pop3邮件保存到本地

    python实现下载pop3邮件保存到本地

    这篇文章主要为大家详细介绍了python实现下载pop3邮件保存到本地的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • 解决Jupyter NoteBook输出的图表太小看不清问题

    解决Jupyter NoteBook输出的图表太小看不清问题

    这篇文章主要介绍了解决Jupyter NoteBook输出的图表太小看不清问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论