Build a Text-to-Speech Converter with Python in 5 Minutes
I have chosen to go through how to build a text-to-speech converter in Python, not only is it simple, but it is also fun and interactive. I will show you two ways you can do it with Python.
Kelly Sikkema
The best thing for your early programming skills is projects. You may have the knowledge, but applying them is the real challenge and keeps you competitive.
I have interviewed a few seniors in recent months and the same thing that they say about newbies is their lack of applying their skills to projects or real-life problems. So with that being said, I thought it would be interesting if I created this quick 5-minute project to help apply and build your skills.
I have chosen to go through how to build a text-to-speech converter in Python, not only is it simple, but it is also fun and interactive. I will show you two ways you can do it with Python.
So let’s get started.
Using pyttsx3
Requirements
For this quick and easy build, you will need the following module: pyttsx3. This module is a text-to-speech conversion library. This module is compatible with Python 2 and 3.
In order to install the module, type this:
pip install pyttsx3
Import
Now you want to import the library into your environment:
import pyttsx3
Engine Instance
We will now initiate the ‘init’ function in order to get an engine instance
engine = pyttsx3.init()
Tell our engine what to say
Using the ‘say’ method on the engine, we input text we wish to be spoken
engine.say('Oh my. I can't believe I did this in less than 5 minutes')
Time to hear it
We now use the ‘run and wait’ method to process the voice commands
engine.runAndWait()
That’s it. Now another one..
Using gTTS API
Requirements
For this text-to-speech converter, we will need the Google Text-to-Speech API. It easily converts inputted text into audio which can then be saved as an mp3 file.
This can be used for several languages such as English, Hindi, Tamil, French, German, and many more.
In order to install the API, type this:
pip install gTTS
Import
Now you want to import the library into your environment:
from gtts import gTTS
You also want to import os in order to play the audio:
import os
Input your text
text = 'Learn how to build something with Python in 5 minutes'
Choose your language
Choose your preferred language. You can find a list of the languages by clicking on this link.
language = 'en'
Passing the text into the engine
You can also choose between the pace of the audio speed being fast or slow.
myobj = gTTS(text=mytext, lang=language, slow=False)
Save your audio as an .mp3
myobj.save("mytext2speech.mp3")
Time to hear it
mpg123 is a free and open-source audio player and we will add it in to mention which program we want our .mp3 file to be played on.
os.system("mpg321 mytext2speech.mp3")
Ta-da! Your chosen media player should have said:
“Learn how to build something with Python in 5 minutes”
Wrapping it up
This article is purely to allow you to explore your Python skills to then go on to building better and cooler projects. & for a little bit of fun!
Nisha Arya is a Data Scientist and Freelance Technical Writer. She is particularly interested in providing Data Science career advice or tutorials and theory based knowledge around Data Science. She also wishes to explore the different ways Artificial Intelligence is/can benefit the longevity of human life. A keen learner, seeking to broaden her tech knowledge and writing skills, whilst helping guide others.