Welcome, guest | Sign In | My Account | Store | Cart

This recipe shows how to generate computer speech from text using the pyttsx Python library. Text-to-speech, or speech synthesis, has many useful applications.

Python, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pyttsx
engine = pyttsx.init()
engine.setProperty('rate', 70)

voices = engine.getProperty('voices')
for voice in voices:
    print "Using voice:", repr(voice)
    engine.setProperty('voice', voice.id)
    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")
engine.runAndWait()

The pyttsx library is a cross-platform wrapper that supports the native text-to-speech libraries of Windows and Linux at least, using SAPI5 on Windows and eSpeak on Linux.

Using it can reduce the amount of OS-specific code you need to write for the task of speech synthesis from your programs.

More details in this blog post:

http://jugad2.blogspot.in/2014/03/speech-synthesis-in-python-with-pyttsx.html

17 comments

James Mills 7 years, 9 months ago  # | flag

I tried your little sample out on my CRUX/Linux desktop at home with espeak :) Very nice, works as expected.

What I find though is that the CLI work just as well...

cheers James

Vasudev Ram (author) 7 years, 9 months ago  # | flag

@James Mills: Glad you like it. Yes, the CLI works as well, but the point is that if you put the text-to-speech functionality in a library, as the author of pyttsx has done (instead of only as a CLI executable), you can include that functionality as part of your own programs (without having to shell out to the executable, which is inefficient, as it has the overhead of creating another process each time you want your program to "speak").

Aaeronn 7 years, 8 months ago  # | flag

I tried this code but it says no module name pyttsx.. I just executed the above code.. any help ??/

Shane Wang 7 years, 5 months ago  # | flag

@Aaeronn, obviously you need install pyttsx first:)

Michael Cooper 7 years ago  # | flag

I installed pyttsx and still get the following error when running this module:

Traceback (most recent call last): File "C:\Python34\voiceTest.py", line 1, in <module> import pyttsx File "C:\Python34\lib\site-packages\pyttsx__init__.py", line 18, in <module> from engine import Engine ImportError: No module named 'engine'

Any ideas would be appreciated. Using Python 3.4.1

Thanks, Michael

Vasudev Ram (author) 7 years ago  # | flag
  1. I only tried it on Python 2, IIRC.

  2. "No module" could imply one of a few possible causes: the module was not installed at all (but you did install it, so this may not applicable) / not installed properly / installed but not configured properly / installed and configured but not made known to the system (i.e. not in the PYTHONPATH or in the right place under $PYTHONBASEDIR/lib/site-packages), etc.

HTH.

sunny chugh 5 years, 11 months ago  # | flag

Traceback (most recent call last): File "C:/Users/sunny/PycharmProjects/untitled/python", line 2, in <module> engine = pyttsx.init() File "C:\Python27\lib\site-packages\pyttsx__init__.py", line 39, in init eng = Engine(driverName, debug) File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__ self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 64, in __init__ self._module = __import__(name, globals(), locals(), [driverName]) File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 19, in <module> import win32com.client ImportError: No module named win32com.client

please help! asap thanks sunny,

Wesam Saleh 5 years, 6 months ago  # | flag

So when I run the code I get the following error.

Traceback (most recent call last): File "ex1.py", line 2, in <module> engine = pyttsx.intit() AttributeError: 'module' object has no attribute 'intit'

Can someone help?

Wesam Saleh 5 years, 6 months ago  # | flag

@sunny chugh

You have to download the extension.

Here is the link. Pick the one that is your system bit (either 32 or 64) and pick the one that is your version of python.

Link: https://sourceforge.net/projects/pywin32/files/pywin32/Build%20220/

Forexample mine was the 3rd to last choice.

Vasudev Ram (author) 5 years, 6 months ago  # | flag

Can someone help?

Check method spelling.

Vasudev Ram (author) 5 years, 6 months ago  # | flag

"Can someone help?"

Check method spelling.

Wesam Saleh 5 years, 6 months ago  # | flag

I want to make the voice id in different accents/languages. How do I go about doing that.

Grayson L. Wiggins 5 years, 1 month ago  # | flag

Im trying to run pyttsx 1.1 on Python 3.5.2 using Spyder and I keep getting this error:

File "C:\Users\grays\Anaconda3\lib\site-packages\pyttsx\driver.py", line 64, in __init__ # import driver module

ImportError: No module named 'drivers'

I have 'fixed' all the others that have popped up, but I can't get through this one. Any suggestions?

Grayson L. Wiggins 5 years, 1 month ago  # | flag

Nevermind, for whatever reason, Spyder didn't know

import driver module

was the same as

'''import driver module'''

I fixed it.

Grayson L. Wiggins 5 years, 1 month ago  # | flag

...actually I didn't...

File "C:\Users\grays\Anaconda3\lib\site-packages\pyttsx\driver.py", line 64, in __init__ '''import driver module'''

ImportError: No module named 'drivers'

Jacob Brewer 4 years, 10 months ago  # | flag

I'm getting this error:

Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> engine = pyttsx.init() File "C:\Python27\lib\site-packages\pyttsx__init__.py", line 39, in init eng = Engine(driverName, debug) File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__ self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 66, in __init__ self._driver = self._module.buildDriver(weakref.proxy(self)) File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 37, in buildDriver return SAPI5Driver(proxy) File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 56, in __init__ self.setProperty('voice', self.getProperty('voice')) File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 98, in setProperty self._tts.Voice = token File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 549, in __setattr__ self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value) com_error: (-2147352573, 'Member not found.', None, None)

I have installed pyttxs and the extension for my version of python (2.7)

Ramisha 4 years, 10 months ago  # | flag

https://youtu.be/ZDDDUMs3iRg How to do Text to speech using python and opencv of webcam image