Saturday, March 1, 2014

Speech synthesis in Python with pyttsx

By Vasudev Ram

[ Click on the link below to hear a synthesized voice:

pyttsx is a library for cross-platform speech synthesis (a.k.a. text-to-speech) in Python. I saw it via this article on Dr. Dobbs Journal:

Voice Throwing with Python and Android.

I have an interest in speech synthesis and have tried out some tools for it in the past.

So I downloaded and installed pyttsx, and then tried it out a bit. Had to tweak the speech rate a bit, since the default "voice" was talking a bit too fast. Also, although going by its API, pyttsx seems to support more than one voice (if your underlying text-to-speech API supports that), the program only played the words in one voice (that of a woman), even though I ran it in a loop for all the voices. It could be that my OS doesn't support more than one voice, or maybe I need to use some other pyttsx option.

Here is the test program using pyttsx,
import pyttsx
engine = pyttsx.init()
engine.setProperty('rate', 70)

voices = engine.getProperty('voices')
for voice in voices:
    print "Using voice:", repr(voice)
    engine.say("Hi there, how's you ?")
    engine.say("A B C D E F G H I J K L M")
    engine.say("N O P Q R S T U V W X Y Z")
    engine.say("0 1 2 3 4 5 6 7 8 9")
    engine.say("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")
    engine.say("Violet Indigo Blue Green Yellow Orange Red")
    engine.say("Apple Banana Cherry Date Guava")

You can run it with the command:

- Vasudev Ram - Dancing Bison Enterprises

Contact Page


Matthias Freiburghaus said...

Does not work for me:

C:\Users\nestor>pip install pyttsx
Collecting pyttsx
Downloading pyttsx-1.1.tar.gz
Building wheels for collected packages: pyttsx
Running bdist_wheel for pyttsx ... done
Stored in directory: C:\Users\nestor\AppData\Local\pip\Cache\wheels\31\74\27\1
Successfully built pyttsx
Installing collected packages: pyttsx
Successfully installed pyttsx-1.1

Traceback (most recent call last):
File "", line 2, in
engine = pyttsx.init()
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\", line 39
, in init
eng = Engine(driverName, debug)
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\", line 45,
in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\", line 66,
in __init__
self._driver = self._module.buildDriver(weakref.proxy(self))
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\drivers\", li
ne 37, in buildDriver
return SAPI5Driver(proxy)
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\drivers\", li
ne 56, in __init__
self.setProperty('voice', self.getProperty('voice'))
File "C:\Users\nestor\Anaconda2\lib\site-packages\pyttsx\drivers\", li
ne 98, in setProperty
self._tts.Voice = token
File "C:\Users\nestor\Anaconda2\lib\site-packages\win32com\client\",
line 549, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352573, 'Mitglied nicht gefunden.', None, None)


Vasudev Ram said...

Well, it worked for me, and for others, see the same code on my ActiveState Code page.
So something may be different / not suitable, at your end.

Anonymous said...

HI there! I guess this only work for English language? Anyway, how would you use it in other languages?

Vasudev Ram said...

Hi, I don't know, sorry. Try googling for appropriate search phrases. - VR.

Vaionescu said...

For error pywintypes.com_error: (-2147352573, 'Mitglied nicht gefunden.', None, None), import these modules at the beginning of the file:

import win32com.client
from win32com.client import constants
from win32com.server.exception import COMException

Vasudev Ram said...

@Vaionescu: Thanks for the tip.

Vasudev Ram said...

Are you looking for training on Python programming, SQL programming and database design, or Unix / Linux architecture, usage, commands and shell scripting?

Visit my training page and check out the courses I offer.

Vasudev Ram said...

I conduct courses on:

- Python programming
- Linux commands & shell scripting
- SQL programming and database design
- PDF report generation using ReportLab and xtopdf

xtopdf is my own product, a Python toolkit for PDF generation from other formats.

Check out my course outlines and testimonials.

More courses will be added over time.

Sign up to be notified of my new courses