Python Text to Speech
In this article, you will learn how to convert text to speech. The text to speech demand is most common. Many people like to listen instead of reading, like news articles, courses and much more. There are several APIs available to convert text to speech. In this article, we will use the Google Text-To-Speech or gTTS module.
The gTTS module produce very natural voice like human while some other APIs provide voice like robotic. It is easy to implement, as it simply converts specified text into audio which we can save as mp3 file. It has capacity to read long length of text with keeping proper intonation, abbreviations, decimals and more. This API also support many languages including English, Hindi, French, German and much more.
gTTS Module Installation
We can install the gTTS module using pip tool. For this, open a terminal window and run the following command -
pip install gTTS
Syntax of gTTS
gTTS(text, lang, slow, lang_check, tokenizer_func)
text (string) - The text to be read,
lang (string, optional) - The language to read the text in. Default is en,
slow (bool, optional) - Reads text more slowly. Defaults to False.
lang_check (bool, optional) - If set to True, a ValueError is raised if lang doesn't exist.
tokenizer_func (callable) - A function that takes in a string and returns a list of string (tokens).
These are the some simple examples of text to speech conversion
Convert text to audio
First, we will import the module and pass the text, voice language and slow attributes values to the gTTS() method.
from gtts import gTTS text = "The gTTS module a Python library and CLI tool to interface with Google Translate's text-to-speech application." language = 'en' speech = gTTS(text = text, lang = language, slow = False) speech.save("text.mp3")
The above code saves the following audio file in the current working directory.
Convert text file to audio
Here is the program to generate an audio file from a text file.
from gtts import gTTS import os f = open('text.txt', 'r') file_text = f.read() f.close() language = 'en' speech = gTTS(text = file_text, lang = language, slow = False) speech.save("text.mp3")
Playing sound directly from a string
We can directly play the converted sound with the help of the os module.
from gtts import gTTS import os text = "Hello Priska Kashyap! How are you?" language = 'en' speech = gTTS(text = text, lang = language, slow = False) speech.save("text.mp3") os.system("text.mp3")
When, we will run the above code, it directly opens the audio player and play the voice.
Related ArticlesHow to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Simple Face Detection Program in Python OpenCV
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples