Text to speech

We can make the computer speak with Python. Given a text string, it will speak the written words in the English language. This process is called Text To Speech or shortly TTS.

Installation

You need to install one of these two modules: pyttsx or gTTS.
You can simply install with pip:

sudo pip install pyttsx
sudo pip install gTTS

Text to speech

Pyttsx text to speech
Pytsx is a cross-platform text-to-speech wrapper. Underlying it uses different speech engines based on your operating system:

  • nsss – NSSpeechSynthesizer on Mac OS X 10.5 and higher
  • sapi5 – SAPI5 on Windows XP, Windows Vista, and (untested) Windows 7
  • espeak – eSpeak on any distro / platform that can host the shared library (e.g., Ubuntu / Fedora Linux)

Example:

import pyttsx
engine = pyttsx.init()
engine.say('Good morning.')
engine.runAndWait()

gTTS text to speech
gTTS is a module and command line utility to save spoken text to mp3 via the Google Text to Speech (TTS) API. This module supports many languages and sounds very natural.

from gtts import gTTS
import os
tts = gTTS(text='Good morning', lang='en')
tts.save("good.mp3")
os.system("mpg321 good.mp3")

If you want to test it on the command line use:

gtts-cli.py "Hello" -l 'en' -o hello.mp3

Read Gmail using Python
Class

One thought on “Text to speech

  1. Reply
    Chandra - February 28, 2017

    Thank you so much. I tried several other translate commands and nothing worked till i landed on your page. Your tutorial worked perfectly. Many Thanks again!!

Leave a Reply

Your email address will not be published. Required fields are marked *