Easy Text-to-Speech with Python

Text-to-speech (TTS) technology reads aloud digital text. It can take words on computers, smartphones, tablets and convert them into audio. Also, all kinds of 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.
Python comes with a lot of handy and easily accessible libraries and we’re going to look at how we can deliver text-to-speech with Python in this article.
Source: https://www.youtube.com/watch?v=eiP-12qHM-c
Different API’s are available in Python in order to convert text to speech. One of Such API’s is the 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 a mp3 file. It supports several languages and the speech can be delivered in any one of the two available audio speeds, fast or slow. More details can be found here

Convert Text into Speech

Code:

Import gTTS library and “os” module in order to play the converted audio
from gtts import gTTS 
import os
Creating a text that we want to convert into audio
text = “Global warming is the long-term rise in the average temperature of the Earth’s climate system”
gTTS supports multiple languages. Please refer to the documentation here. Selected ‘en’ -> English and stored in the language variable
language = ‘en’
Creating an object called speech and passing the text and language to the engine. Marked slow = False which tells the module that the converted audio should have a high speed.
speech = gTTS(text = text, lang = language, slow = False)
Saving the converted audio in a mp3 file named called ‘text.mp3’
speech.save(“text.mp3”)
Playing the converted file, using Windows command ‘start’ followed by the name of the mp3 file.
os.system(“start text.mp3”)

Output

text.mp3 file
The output of the above program saved as text.mp3 file. Mp3 file should be a voice saying, 'Global warming is the long-term rise in the average temperature of the Earth’s climate system'

Convert a Text File into Speech

Here, covert the text file into speech. Reading the text file and pass to gTTS module

Code

Import gTTS and os library
from gtts import gTTS 
import os
Reading the text file and store into object called text. My file name is “draft.txt”
file = open("draft.txt", "r").read().replace("\n", " ")
Choosing language English
language = ‘en’
Passing the text file into gTTS module and store into speech
speech = gTTS(text = str(file), lang = 'language', slow = False)
Saving the converted audio in a mp3 file named called ‘voice.mp3’
speech.save("voice.mp3")
Playing the mp3 file
os.system("start voice.mp3")

Output

Converted draft.txt file into voice.mp3
Draft.txt file saved as a voice.mp3 file.Play the Mp3 file to listen the text presented in the draft.txt file

Note:

GTTS is an easy tool to convert text to voice, but it requires an internet connexion to operate because it depends entirely on Google to get the audio data.
Thanks for reading. Keep learning and stay tuned for more!

Towards Data Science

Sharing concepts, ideas, and codes.

You're following Towards Data Science.

You’ll see more from Towards Data Science across Medium and in your inbox.

Aug 20, 2018 · 1 min read
Converting Text into Speech is extremely easy. Lets see a simple example, how to convert a given text into an automated voice —
Step#1: Install gtts library. Execute pip install gtts on Anaconda Shell, as we will be using Jupyter Notebook.
Step#2: On Jupyter Notebook, write the below code. The mentioned code demonstrates how a given text “Good morning” is converted into English automated voice. Also if we have a text which is not in English, but in any other language (let say in Hindi), gtts can also convert it well !! :)
Step#3: Now check the generated files in the desired location:
Hope this quick tutorial helps. Thanks :)

Comments

Popular posts from this blog

Flutter for Single-Page Scrollable Websites with Navigator 2.0

A Data Science Portfolio is More Valuable than a Resume

Better File Storage in Oracle Cloud