Hello friends, now a day there are lots of trends for bots and Microsoft Cortona , Google Assistant and Apple’s Siri kind of assistants, if you notice all has one thing common that they process our speech to text and provide suitable output and convert their output into speech. So it makes more user friendly and fun. 🙂
Today we are going to do little demo of text to Speech using Python and Google’s library for Text to Speech gTTS
First of all we have to install gTTS library using pip install command as mentioned below
pip install gTTS
Once downloading is complete, we can test it in our python shell by running below commands
>>> from gtts import gTTS >>> tts = gTTS('hello') >>> tts.save('hello.mp3')
It will make one mp3 file under same directory where you are running program. Try to run that file and it will speak out text ‘Hello’ !!! That’s interesting right ? let’s write complete code for text to speech.
import os from gtts import gTTS Text = "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." print("please wait...processing") TTS = gTTS(text=Text, lang='en-in') # Save to mp3 in current dir. TTS.save("voice.mp3")
Finally we have our python program which converts our text into mp3 speech.
Parameter lang= has multiple options for language like en , en-in, fr and others. for more details you can read docs under this link
You can find voice.mp3 file under your directory and try to run it. Hope you like this tutorials. find some ways to build something around this functionality like kind of Jarvis (Personal assistant) 🙂