Python is an incredibly strong and flexible language used for multi-purpose programming. There are a lot of things possible using Python one of them is Text to Speech conversion.
In this tutorial, we will go through the conversion of text to speech using Python and Google’s Text-To-Speech service. Google Cloud Text-to-Speech converts text into human-like speech in more than 100 voices across 20+ languages and variants.
It applies groundbreaking research in speech synthesis (WaveNet) and Google’s powerful neural networks to deliver high-fidelity audio. With this easy-to-use API, you can create lifelike interactions with your users that transform customer service, device interaction, and other applications.
We are going to use a Python package called gTTS (Google Text-to-Speech) for this purpose.
gTTS (Google Text-to-Speech), is a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Which writes spoken
mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or
It features flexible pre-processing and tokenizing, as well as automatic retrieval of supported languages. The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more.
Let’s being with installing the gTTS package first.
pip install gtts
After the installation is done you can go ahead run the following Scripts.
Note that gTTS which works perfectly in python but it needs an internet connection to work since it relies on Google to get the audio data so your machine must have a working internet connection.
Python Program to convert a text into speech
import os from gtts import gTTS Text = "I love Django central and Python programming Language" print("please wait...processing") TTS = gTTS(text=Text, lang='en-uk') # Save to mp3 in current dir. TTS.save("voice.mp3") # Plays the mp3 using the default app on your system # that is linked to mp3s. os.system("start voice.mp3")
Save the file and run it this should open your default music player app and play the audio.
But what if you wanted to convert a large string or document into speech for that you might wanna save the content into a text file then convert it into speech as follows.
Python program to Convert a text file into speech
import os from gtts import gTTS # You will need a text file named test.txt # to play, put it in the current dir. FLIST = open("test.txt", "r").read().replace("\n", " ") print("please wait...processing") TTS = gTTS(text=str(FLIST), lang='en-uk') # Save to mp3 in current dir. TTS.save("voice.mp3") # Plays the mp3 using the default app on your system # that is linked to mp3s. print(FLIST) os.system("start voice.mp3")
Save and run the file you should get the desired output.