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.')

voiceEngine.runAndWait()


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


References

[1] https://pypi.python.org/pypi/pypiwin32

[2] http://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api

[3] https://pyttsx.readthedocs.io/en/latest/engine.html?highlight=init#pyttsx.init

[4] https://pyttsx.readthedocs.io/en/latest/engine.html?highlight=say#pyttsx.engine.Engine.runAndWait


Technical details

  • Python version: 2.7.8
  • Pyttsx version: 1.1

3 thoughts on “Python: Speech Synthesis”

  1. Pingback: Python pyttsx: Changing speech rate and volume | techtutorialsx

  2. Pingback: Python pyttsx: Changing speech rate and volume | techtutorialsx

  3. 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\__init__.py”, line 18, in
    from engine import Engine
    ModuleNotFoundError: No module named ‘engine’

    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

  4. 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\__init__.py”, line 18, in
    from engine import Engine
    ModuleNotFoundError: No module named ‘engine’

    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

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading