How to turn text into speech?
This insight will help you to turn text into speech in just a few lines of python code.
Python is one of the popular languages in our world today, it helps us to develop powerful web applications, Machine Learning and Artificial Intelligence applications, scientific computing applications and many other wonderful applications.
Python can be used to automate repeated tasks easily thus improving productivity. In this insight, we are going to use python to convert text into voice.
This has huge applications overall, you can use this implementation to convert books into audio, you can also use it to convert your emails to audio and listen to them while you are doing something else.
Let’s see how this can be done.
There are many APIs that can be used to convert text to speech in python. In this example, we will use the GOOGLE TEXT TO SPEECH API commonly known as the gTTS API. Using this library, we will be able to save the converted text into an Audio mp3 file.
To make it more interesting, The Google Text To Speech API supports many other languages including: English, French, Italia, Spanish, Hindi, Japanese, German, Tamil, Portuguese and many others.
You need to install Python on your computer, you also need to install pip, a python package manager. Having done that, proceed to write the following program in your favorite text editor or Integrated Development Environment.
from gtts import gTTS mytext = "The Arkiana blog is Insightful. I love it." language = 'en' myObject = gTTS(text=mytext, lang=language, slow=False) myObject.save("ConvertedFile.mp3")
You should have a file created in your project directory called ConvertedFile.mp3 which should say, “The Arkiana blog is Insightful. I love it.” when you play it.
Its that easy.
Your can also ask for user input and convert it to speech by just changing a few lines like so:
from gtts import gTTS mytext = input('Write the text that will be converted to Voice ') language = 'en' myObject = gTTS(text=mytext, lang=language, slow=False) myObject.save("SpeechFile.mp3")
So simple right. That’s it.
Go ahead and experiment with code. Make some changes with the language, pace of speech and more. You can also try out other Text to Speech libraries.
You can learn more about many other projects that you can automate in the Book Automate the Boring Stuff With Python