Speech engines with python tutorial

Text To Speech (TTS)
Text To Speech (TTS)
A computer system used to create artificial speech is called a speech synthesizer, and can be implemented in software or hardware products.

A text-to-speech (TTS) system converts normal language text into speech. How can we use speech synthesis in Python?

Related courses:


Pyttsx is a cross-platform speech (Mac OSX, Windows, and Linux) library. You can set voice metadata such as age, gender, id, language and name. Thee speech engine comes with a large amount of voices.

Text to speech sample:

Install with:

sudo pip install pyttsx

Create the code speech1.py

import pyttsx
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')

And execute it with python.


eSpeak is a compact open source software speech synthesizer for English and other languages, for Linux and Windows.

Text to speech sample:

We can install using:

sudo apt-get install espeak

Create the code speech2.py:

import os
os.system("espeak 'The quick brown fox'")

It is very easy to use, but like pyttsx it sounds very robotic.


The gtts module no longer works.

I found a script on Github that uses the Google speech engine. The script comes with many options and does not speak, instead it saves to an mp3. We added a command to play the mp3 automatically:

os.system("mpg321 out.mp3 -quiet")

Run with:

python gtts.py -s 'Python programming example'

The voice is extremely natural. The only disadvantage is that you need to be connected with the Internet when running this script.


You might like:

String methods

6 thoughts on “Speech engines with python tutorial

  1. Suhrid Das - August 23, 2017

    when I use the pyttsx code it gives error:
    import pyttsx
    File “C:\Users\Suhrid’s\AppData\Local\Programs\Python\Python36\lib\site-packages\pyttsx\__init__.py”, line 18, in
    from engine import Engine
    ModuleNotFoundError: No module named ‘engine’
    how to fix?

    1. Frank - August 24, 2017

      For pyttsx with Python 3 you need a patched version. This one might do: https://github.com/AMairesse/pyttsx

  2. Yusuf Adeyemo - August 8, 2017

    Can Espeak read from text file? If yes, How do I go about that

    1. Frank - August 8, 2017

      Espeak itself not, but Python can. Read the file line by line, then give each line to espeak in a for loop. This method will work with any speech system.

  3. Anjan Dudda - December 28, 2016

    How do i change the language in pyttsx from english to german? It would be great if you can send me some sample code for the same. Thanks in advance.

    1. Frank - January 8, 2017

      It depends on the driver, see available drivers on: https://github.com/RapidWareTech/pyttsx/tree/1a84ee33971951b1ea18f2708061a5d19ef94018/pyttsx/drivers

      If you use Espeak you could try the setProperty method and set ‘languages’