All Courses

Text-to-Speech with Python using Google Text-to-Speech

Neha Kumawat

2 years ago

Text-To-Speech With Python Using Google | insideAIML 
Table of Contents
  • What is Text-to-speech?
  • How to Convert Text into Speech?

What is Text-to-speech?

          Text-to-speech (TTS) technology reads digital text aloud. It is a type of assistive technology. It is also known as “read aloud” technology.
Its so easy that with a click of a button or the touch of a finger, it can take words on a computer, smartphones, tablets and convert them into audio.  TTS can help kids who struggle with reading and it also helps kids in writing and editing.
Different text files can be read aloud, including Word, pages document, online web pages can be read aloud. TTS can help kids who struggle with reading. Many tools and apps are available to convert text into speech.
Some of the benefits of Text-to-speech are as mentioned below:
  • TTS improves word recognition.
  • TTS improves the ability to remember information while reading.
  • TTS helps users to recognize and fix errors in their own writing.
Python provides some of the handy and easily accessible libraries.
Let’s see how we can implement Text-to-Speech in python using Google Text-to-Speech (gtts).
Google Text to Speech commonly known as the gTTS API. It is very easy to use the library which converts the text entered, into an audio file which can be saved as an mp3 file. It supports several languages and the speech can be delivered in any one of the two available audio speeds, fast or slow.

How to Convert Text into Speech?

Note: You have to download and install gTTS into your computer. It can be done as:
pip install gTTS
#import “gTTS” library and “os”
from gtts import gTTS
import os
Create a text that we want to convert into audio
text = “InsideAIML, World's Best AI Learning Platform with Profoundly Demanding Certification Programs”
Google TTS supports many different languages. Here we will be using the English language. It can be implemented as shown below-
language = “en”
Now next, we have to create an object called speech here and then passing the text and language to the engine. Here Marked slow = false which tells python that the converted audio should have a high speed. It can be done as shown below-
speech = gTTS(text = text, lang = language, slow = false)
Now, let’s save the converted audio in a mp3 file named called ‘speech_to_text.mp3’“speech_to_text.mp3”)
As the text is converted and saved. Now, its time to play it. It can be done as shown below-
os.system(“start speech_to_text.mp3”)
Like the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our blog page -
Keep Learning. Keep Growing. 

Submit Review