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