Start Learning Python today at Teach Me Python

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:

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.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:

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.

Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary