Python Text to Speech
In this article, you will learn how to convert text to speech using the Python programming language. 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.
gtts Python
The gTTS module produces a very natural voice, like a human, while some other APIs provide voice like a robotic. It is easy to implement as it simply converts specified text into audio, which we can save as an mp3 file. It has the capacity to read a long length of text while keeping proper intonation, abbreviations, decimals, and more. This API also supports many languages, including English, Hindi, French, German and much more.
gTTS Module Installation
We can install the gTTS module using the 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 strings (tokens).
Python gTTS Examples
These are 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 attribute 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.
Python 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")
Python 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 Articles
How to capture a video in Python OpenCV and savePython OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
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