Python: Speech Synthesis

The objective of this post is to explain how to synthesis a simple speech using Python and the pyttsx module.

Installing the module

We will use pip to install the pyttsx module. Nevertheless, as indicated here, we first need to install the the pywin32-extensions package. Although we can do it using an installer available here, the easiest way is also installing via pip, using the following command:

pip install pypiwin32

Just as a quick explanation, pywin32 provides access to part of the Win32 API [1].

After this installation, we just need to install pyttsx with the following pip command:

pip install pyttsx

If after the installation, upon running an example, you get the error bellow, just restart the Python shell / editor and try to run it again [2].

ImportError: no module named win32api

Hello world program

First, we start by importing the previously installed pyttsx module.

import pyttsx

After the initial import, we call the init function to get an engine instance for the speech synthesis [3]. If we don’t pass any argument as input of the function, it will use the best driver for the operating system we are using [3].

voiceEngine = pyttsx.init()

Then, we call the say method on the engine instance, passing as input the text to be spoken.

voiceEngine.say('Hello World! This is a speech synthesis test.')

Finally, we call the runAndWait method, also on the engine instance, to process the voice commands [4]. Check the full code, already with this call, bellow.

import pyttsx

voiceEngine = pyttsx.init()
voiceEngine.say('Hello World! This is a speech synthesis test.')


Testing the code

To test the code, just run it, for example, on IDLE. The string defined in the code should now be synthesized and spoken, as indicated in the video bellow. Please note that different versions of the operating system may result in different voices, depending on the underlying speech synthesis engine. Also, in the video bellow, the voice is kind of robotized, probably due to my screen recording software. On my computer, the output is very clean.

Related content






Technical details

  • Python version: 2.7.8
  • Pyttsx version: 1.1

3 Replies to “Python: Speech Synthesis”

  1. i keep getting this error
    Traceback (most recent call last):
    File “”, line 1, in
    import pyttsx
    File “C:\Users\Emmanuel GODWIN\AppData\Local\Programs\Python\Python36\lib\site-packages\pyttsx\”, line 18, in
    from engine import Engine
    ModuleNotFoundError: No module named ‘engine’

    Liked by 1 person

    1. Hi!

      I never ran into that issue so unfortunately I cannot help much.

      My suggestion is that you try to share the problem at the GitHub page of the module, so maybe someone else who has experienced the issue can help.

      Let us know if you find a solution.

      Best regards,
      Nuno Santos


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s