Python Text-to-Speech: Making Your PC Talk

Soon after getting hired at my current job, my boss sent me a script (which I think was based on this article) about Python and a certain text-to-speech module called pyTTS. This was right after Python 2.5 had released. Anyway, it’s basically a nice wrapper over the win32com module which can communicate with the Microsoft Speech API (SAPI).

I won’t go over everything about pyTTS since that article I linked to above already does that, but I will give you a quick introduction to it. If you go to the author’s website you’ll find that he’s moved on to Screen Reading technology and has created a program called Clique. I’m not sure if that is written in Python or not though. I went digging on the site to find out where you could download the software and finally found this: http://sourceforge.net/projects/uncassist/files/

From what I can tell, he only officially support Python 2.3-2.5. However, since pyTTS basically just wraps the win32com calls to SAPI and the PyWin32 module supports Python 2.x-3.x, I think it would be fairly easy to make pyTTS work with the newer versions.

Note: You will need the Microsoft SAPI 5.1 redistributable, extra MS voices and PyWin32.

Let’s take a quick look at how we use this module:

import pyTTS
tts = pyTTS.Create()
tts.SetVoiceByName('MSSam')
tts.Speak("Hello, fellow Python programmer")

You don’t have to set the voice as I think the default it MSMary, but it’s fun to do. The Speak method accepts various flags for its second argument. One example is pyTTS.tts_async, which puts the speaking in asynchronous mode. You can also mess with the volume by doing this: tts.Volume = 50. You can choose anywhere from 0-100%.

If you look at this article, it will teach you how to make pyTTS pronounce the words you feed it.

Here is how you would do most of the example above using PyWin32:

from win32com.client import constants
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice", constants.SVSFlagsAsync)
speaker.Speak("Hello, fellow Python programmer")

While researching this article, I noticed that the developer(s) behind pyTTS had also put together a cross-platform text-to-speech module called pyttsx. I haven’t used it, but I encourage you to give it a try. Other modules worth a look are pySpeech and this cool recipe that allows Python to recognize speech: http://www.surguy.net/articles/speechrecognition.xml

Well, this ended up being more of a survey of the cool speech-related modules in Python than about code. Still, I hope this will prove helpful to you in your endeavors.

7 thoughts on “Python Text-to-Speech: Making Your PC Talk”

  1. FYI: you have “test” in the title reather than “text”. I was thinking you implemented this as a unittest extension to yell at you when tests fail 🙂

  2. FYI: you have “test” in the title reather than “text”. I was thinking you implemented this as a unittest extension to yell at you when tests fail 🙂

  3. Hi Brian,

    Hmmm…my proof-reading skills failed me on that one. Good catch. I’ve fixed that issue now.

    – Mike

  4. Hi Brian,

    Hmmm…my proof-reading skills failed me on that one. Good catch. I’ve fixed that issue now.

    – Mike

  5. pyttsx seems to lack a non-blocking mode, so a thread might be necessary.
    To use most of the features of pytts with python 2.6, I have copied the pyTTS folder from the site-packages of another version of python, and replaced TTSFast.py with an empty file so the incompatible _TTSFast.pyd is not imported anymore.

Comments are closed.