python实现将文本转换成语音的方法
本文实例讲述了python将文本转换成语音的方法。分享给大家供大家参考。具体实现方法如下:
#TextToSpeechusingSAPI(Windows)andPythonmodulepyTTSbyPeterParente
#downloadinstallerfilepyTTS-3.0.win32-py2.4.exe
#from:http://sourceforge.net/projects/uncassist
#alsoneeds:http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi
#andpywin32-204.win32-py2.4.exeatthisdatethelatestversionofwin32com
#from:http://sourceforge.net/projects/pywin32/
#testedwithPython24onaWindowsXPcomputervagaseat15jun2005
importpyTTS
importtime
tts=pyTTS.Create()
#setthespeechrate,highervalue=faster
#justforfuntryvaluesof-10to10
tts.Rate=1
print"Speechrate=",tts.Rate
#setthespeechvolumepercentage(0-100%)
tts.Volume=90
print"Speechvolume=",tts.Volume
#getalistofalltheavailablevoices
print"Listofvoices=",tts.GetVoiceNames()
#explicitlysetavoice
tts.SetVoiceByName('MSMary')
print"VoiceissetotMSMary"
print
#announcethedateandtime,doesagoodjob
timeStr="Thedateandtimeis"+time.asctime()
printtimeStr
tts.Speak(timeStr)
print
str1="""
Ayoungexecutivewasleavingtheofficeat6pmwhenhefound
theCEOstandinginfrontofashredderwithapieceofpaperinhand.
"Listen,"saidtheCEO,"thisisimportant,andmysecretaryhasleft.
Canyoumakethisthingwork?"
"Certainly,"saidtheyoungexecutive.Heturnedthemachineon,
insertedthepaper,andpressedthestartbutton.
"Excellent,excellent!"saidtheCEOashispaperdisappearedinside
themachine."Ijustneedonecopy."
"""
printstr1
tts.Speak(str1)
tts.Speak('Haahhaahaahhaa')
print
str2="""
Finagle'sfourthlaw:
Onceajobisfouledup,anythingdonetoimproveitonlymakesitworse.
"""
printstr2
print
print"Thespokentextabovehasbeenwrittentoawavefile(.wav)"
tts.SpeakToWave('Finagle4.wav',str2)
print"Thewavefileisloadedbackandspoken..."
tts.SpeakFromWave('Finagle4.wav')
print
print"SubstituteahardtopronouncewordlikeCtrlkey..."
#createaninstanceofthepronunciationcorrector
p=pyTTS.Pronounce()
#replacewordsthatarehardtopronouncewithsomethingthat
#isspelledoutormisspelled,butatleastsoundslikeit
p.AddMisspelled('Ctrl','Control')
str3=p.Correct('PleasepresstheCtrlkey!')
tts.Speak(str3)
print
print"2*3=6"
tts.Speak('2*3=6')
print
tts.Speak("soundsgoofy,let'sreplace*withtimes")
print"Substitute*withtimes"
#'*'needsthespaces
p.AddMisspelled('*','times')
str4=p.Correct('2*3=6')
tts.Speak(str4)
print
print"Saythatrealfastafewtimes!"
str5="Thesinkingsteamersunk!"
tts.Rate=3
forkinrange(7):
printstr5
tts.Speak(str5)
time.sleep(0.3)
tts.Rate=0
tts.Speak("Wow,notonemispronouncedword!")
希望本文所述对大家的Python程序设计有所帮助。