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程序设计有所帮助。

相关文章

  • Numpy中的shape、reshape函数的区别

    Numpy中的shape、reshape函数的区别

    本文主要介绍了Numpy中的shape、reshape函数的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Python Pandas学习之数据离散化与合并详解

    Python Pandas学习之数据离散化与合并详解

    Pandas是python的一个数据分析包,该工具是为解决数据分析任务而创建的。本文将通过示例详细为大家介绍一下Pandas的数据离散化与合并,需要的可以参考一下
    2022-02-02
  • 使用OpenCV实现仿射变换—平移功能

    使用OpenCV实现仿射变换—平移功能

    这篇文章主要介绍了使用OpenCV实现仿射变换—平移功能,需要的朋友可以参考下
    2019-08-08
  • Python学习笔记(一)(基础入门之环境搭建)

    Python学习笔记(一)(基础入门之环境搭建)

    本系列为Python学习相关笔记整理所得,IT人,多学无害,多多探索,激发学习兴趣,开拓思维,不求高大上,只求懂点皮毛,作为知识储备,不至于落后太远。本文主要介绍Python的相关背景,环境搭建。
    2014-06-06
  • Python文件监听工具pyinotify与watchdog实例

    Python文件监听工具pyinotify与watchdog实例

    今天小编就为大家分享一篇关于Python文件监听工具pyinotify与watchdog实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • Python中encode和encoding的区别小结

    Python中encode和encoding的区别小结

    encode和encoding在Python中虽然都与字符编码相关,但它们的角色和用途是不同的,本文主要介绍了Python中encode和encoding的区别小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • jupyter notebook 参数传递给shell命令行实例

    jupyter notebook 参数传递给shell命令行实例

    这篇文章主要介绍了jupyter notebook 参数传递给shell命令行实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python动态导入模块的方法实例分析

    Python动态导入模块的方法实例分析

    这篇文章主要介绍了Python动态导入模块的方法,结合实例形式较为详细的分析了Python动态导入系统模块、自定义模块以及模块列表的相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • Python3.10新特性之match语句示例详解

    Python3.10新特性之match语句示例详解

    这篇文章主要为大家介绍了Python3.10新特性之match语句示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 使用python实现mqtt的发布和订阅

    使用python实现mqtt的发布和订阅

    这篇文章主要介绍了使用python实现mqtt的发布和订阅,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05

最新评论