使用python把文本转化为声音

本文介绍如何使用Python在Windows环境下实现文本到语音的转换。通过安装必要的组件如Python 2.5、pywin32、SAPI5及pyTTS等,可创建一个简单的人机声音交互环境。文中提供了具体的安装步骤和示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用python把文本转化为声音
作者:枯藤天涯 转自:http://bbs.bccn.net/thread-258434-1-1.html


目的:
建立一个人机声音交互的可编程的环境
前提:
1.操作系统:winxp,4元一张
2.python解释器:python25,python-2.5.2.msi
3.python的windows扩展:pywin32-212.win32-py2.5.exe
4.微软声音识别引擎:
SAPI5VoiceInstaller.msi(WIN XP本身已安装好)
SAPI5SpeechInstaller.msi(WIN XP本身已安装好)
5.对微软声音识别引擎的python包装pyTTS:pyTTS-3.0.win32-py2.5.exe
注意:
在网上搜索的时候,注意版本的区别。上面是我使用的版本,是可以建立的。

步骤:
1.安装python-2.5.2.msi
2.安装pywin32-212.win32-py2.5.exe
它会找到python安装的目录。
3.安装微软声音识别引擎:
4.安装pyTTS,
会自动的找到python的安装目录.
注意:安装的时候注意安装的顺序。按照这个顺序安装,可以省去手动找目录的麻烦。

检测:
import pyTTS
tts=pyTTS.Create()
tts.Speak("I love China")
这时你就会听到I love China的声音了。
当然还可以做的更多。


# to bring text to speech capability to your Windows computer# install SAPI5Speech (XP and Vista should have it already)# http://www.nolad.com/vt/redist/SAPI5SpeechInstaller.msi## Windows XP has Sam, SAPI5Voice adds Mary and Mike# http://www.nolad.com/vt/redist/SAPI5VoiceInstaller.msi## the Python for Windows extensions should be installed# for COM to work eg. pywin32-210.win32-py2.5.exe# from http://sourceforge.net/projects/pywin32/## tested with Python25 on a Windows XP machine by vegaseat import win32com.client voices = {'Sam' : 'Microsoft Sam','Mary' : 'Microsoft Mary','Mike' : 'Microsoft Mike'} # choose voice from the voices dictionaryvoice = 'Sam'# range 0(low) - 100(loud)volume = 100# range -10(slow) - 10(fast)rate = -1 # some text to speaktext = """\It is said, that if you line up all the cars in the world end to end, someone would be stupid enough and try to pass them.""" # initialize COM components of MS Speech API# COM is Microsoft's Component Object Model# (COM is also used by Peter Parente's pyTTS)speak = win32com.client.Dispatch('Sapi.SpVoice')# assign a voicespeak.Voice = speak.GetVoices('Name='+voices[voice]).Item(0)speak.Rate = ratespeak.Volume = volume# now speak out the textspeak.Speak(text)# to bring text to speech capability to your Windows computer
# install SAPI5Speech (XP and Vista should have it already)
# http://www.nolad.com/vt/redist/SAPI5SpeechInstaller.msi
#
# Windows XP has Sam, SAPI5Voice adds Mary and Mike
# http://www.nolad.com/vt/redist/SAPI5VoiceInstaller.msi
#
# the Python for Windows extensions should be installed
# for COM to work eg. pywin32-210.win32-py2.5.exe
# from http://sourceforge.net/projects/pywin32/
#
# tested with Python25 on a Windows XP machine by vegaseat

import win32com.client

voices = {
'Sam' : 'Microsoft Sam',
'Mary' : 'Microsoft Mary',
'Mike' : 'Microsoft Mike'
}

# choose voice from the voices dictionary
voice = 'Sam'
# range 0(low) - 100(loud)
volume = 100
# range -10(slow) - 10(fast)
rate = -1

# some text to speak
text = """\
It is said, that if you line up all the cars in the world end to end,
someone would be stupid enough and try to pass them.
"""

# initialize COM components of MS Speech API
# COM is Microsoft's Component Object Model
# (COM is also used by Peter Parente's pyTTS)
speak = win32com.client.Dispatch('Sapi.SpVoice')
# assign a voice
speak.Voice = speak.GetVoices('Name='+voices[voice]).Item(0)
speak.Rate = rate
speak.Volume = volume
# now speak out the text
speak.Speak(text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值