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

相关文章

  • wxpython绘制音频效果

    wxpython绘制音频效果

    这篇文章主要为大家详细介绍了wxpython绘制音频效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • python一行代码就能实现数据分析的pandas-profiling库

    python一行代码就能实现数据分析的pandas-profiling库

    这篇文章主要为大家介绍了python一行代码就能实现数据分析的pandas-profiling库,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Python中itertools的用法详解

    Python中itertools的用法详解

    循环器(iterator)是对象的容器,包含有多个对象。这篇文章主要介绍了python itertools用法,需要的朋友可以参考下
    2020-02-02
  • 使用Pytorch实现Swish激活函数的示例详解

    使用Pytorch实现Swish激活函数的示例详解

    激活函数是人工神经网络的基本组成部分,他们将非线性引入模型,使其能够学习数据中的复杂关系,Swish 激活函数就是此类激活函数之一,在本文中,我们将深入研究 Swish 激活函数,提供数学公式,探索其相对于 ReLU 的优势,并使用 PyTorch 演示其实现
    2023-11-11
  • Python鼠标事件及坐标获取窗口和屏幕坐标

    Python鼠标事件及坐标获取窗口和屏幕坐标

    这篇文章主要介绍了Python编程中如何通过鼠标事件及坐标获取窗口坐标和屏幕坐标的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-10-10
  • Python WSGI的深入理解

    Python WSGI的深入理解

    这篇文章主要给大家介绍了关于Python WSGI的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • Python实现快速多线程ping的方法

    Python实现快速多线程ping的方法

    这篇文章主要介绍了Python实现快速多线程ping的方法,实例分析了Python多线程及ICMP数据包的发送技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • Python调用两个机器人聊天的实战

    Python调用两个机器人聊天的实战

    本文主要介绍了Python调用两个机器人聊天,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Python设置和解除Word文档保护的实现步骤

    Python设置和解除Word文档保护的实现步骤

    在日常工作和学习中,我们经常需要使用Word文档来记录和分享重要的信息,为了确保文档内容的安全性和完整性,了解如何保护和取消保护Word文档显得尤为重要,这篇博客将详细介绍如何使用Python设置和解除Word文档的保护,需要的朋友可以参考下
    2025-02-02
  • Python中闭包与lambda的作用域解析

    Python中闭包与lambda的作用域解析

    这篇文章主要介绍了Python中闭包与lambda的作用域解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论