942,790 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 23525
  • Python RSS
0

Text to Speech in Python

by on Jul 16th, 2005
Yes, you can let your computer read text to you. The task is relatively easy, if you have Windows on your machine. All you need is Microsoft's speech-API SAPI, the Python Text to Speech module pyTTS, and an updated version of win32com, all free downloads. Here are some experiments with the pyTTS module that literally talk to you.
Python Code Snippet (Toggle Plain Text)
  1. # Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente
  2. # download installer file pyTTS-3.0.win32-py2.4.exe
  3. # from: http://sourceforge.net/projects/uncassist
  4. # also needs: http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi
  5. # and pywin32-204.win32-py2.4.exe at this date the latest version of win32com
  6. # from: http://sourceforge.net/projects/pywin32/
  7. # tested with Python24 on a Windows XP computer vagaseat 15jun2005
  8.  
  9. import pyTTS
  10. import time
  11.  
  12. tts = pyTTS.Create()
  13.  
  14. # set the speech rate, higher value = faster
  15. # just for fun try values of -10 to 10
  16. tts.Rate = 1
  17. print "Speech rate =", tts.Rate
  18.  
  19. # set the speech volume percentage (0-100%)
  20. tts.Volume = 90
  21. print "Speech volume =", tts.Volume
  22.  
  23. # get a list of all the available voices
  24. print "List of voices =", tts.GetVoiceNames()
  25.  
  26. # explicitly set a voice
  27. tts.SetVoiceByName('MSMary')
  28. print "Voice is set ot MSMary"
  29.  
  30. print
  31.  
  32. # announce the date and time, does a good job
  33. timeStr = "The date and time is " + time.asctime()
  34. print timeStr
  35. tts.Speak(timeStr)
  36.  
  37. print
  38.  
  39. str1 = """
  40. A young executive was leaving the office at 6 pm when he found
  41. the CEO standing in front of a shredder with a piece of paper in hand.
  42.  
  43. "Listen," said the CEO, "this is important, and my secretary has left.
  44. Can you make this thing work?"
  45.  
  46. "Certainly," said the young executive. He turned the machine on,
  47. inserted the paper, and pressed the start button.
  48.  
  49. "Excellent, excellent!" said the CEO as his paper disappeared inside
  50. the machine. "I just need one copy."
  51. """
  52. print str1
  53. tts.Speak(str1)
  54. tts.Speak('Haah haa haah haa')
  55.  
  56. print
  57.  
  58. str2 = """
  59. Finagle's fourth law:
  60. Once a job is fouled up, anything done to improve it only makes it worse.
  61. """
  62. print str2
  63. print
  64. print "The spoken text above has been written to a wave file (.wav)"
  65. tts.SpeakToWave('Finagle4.wav', str2)
  66.  
  67. print "The wave file is loaded back and spoken ..."
  68. tts.SpeakFromWave('Finagle4.wav')
  69.  
  70. print
  71.  
  72. print "Substitute a hard to pronounce word like Ctrl key ..."
  73. #create an instance of the pronunciation corrector
  74. p = pyTTS.Pronounce()
  75. # replace words that are hard to pronounce with something that
  76. # is spelled out or misspelled, but at least sounds like it
  77. p.AddMisspelled('Ctrl', 'Control')
  78. str3 = p.Correct('Please press the Ctrl key!')
  79. tts.Speak(str3)
  80.  
  81. print
  82.  
  83. print "2 * 3 = 6"
  84. tts.Speak('2 * 3 = 6')
  85.  
  86. print
  87.  
  88. tts.Speak("sounds goofy, let's replace * with times")
  89. print "Substitute * with times"
  90. # ' * ' needs the spaces
  91. p.AddMisspelled(' * ', 'times')
  92. str4 = p.Correct('2 * 3 = 6')
  93. tts.Speak(str4)
  94.  
  95. print
  96.  
  97. print "Say that real fast a few times!"
  98. str5 = "The sinking steamer sunk!"
  99. tts.Rate = 3
  100. for k in range(7):
  101. print str5
  102. tts.Speak(str5)
  103. time.sleep(0.3)
  104.  
  105. tts.Rate = 0
  106. tts.Speak("Wow, not one mispronounced word!")
  107.  
Comments on this Code Snippet
Aug 25th, 2005
0

Re: Text to Speech in Python

Can be shaped into a very helpful tool for any blind person you know!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Sep 1st, 2006
0

Re: Text to Speech in Python

impressive...*has never used python b4* i like it alot!:cheesy:

so...do i just paste this into python interactive window?
Junior Poster in Training
The Geeky Kid is offline Offline
57 posts
since Aug 2006
Sep 1st, 2006
0

Re: Text to Speech in Python

I typed it in, got no errors, but no sound or verification that it worked either...
Junior Poster in Training
The Geeky Kid is offline Offline
57 posts
since Aug 2006
Sep 15th, 2006
0

Re: Text to Speech in Python

Generally you would copy and paste the code into a Python editor like IDLE or DrPython and then run it from there. The sound does use the sound chip and external speaker system of the PC.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Dec 25th, 2009
0

Re: Text to Speech in Python

hello there dude...very good tutorial and inspiring for more projects using pyTTS...but i have a problem...when typing pyTTS.Create() , a ValueError exception is raised : "SAPI not supported" , i have windows xp sp2 , python 2.5 , and installed the first two packages you listed , plus microsoft voices , but still get the same error message , what do you recommend ?
Newbie Poster
personx121232 is offline Offline
3 posts
since Dec 2009
Message:
Previous Thread in Python Forum Timeline: how do I extract data from html file ?
Next Thread in Python Forum Timeline: 8 Directional Movement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC