Convert Text to Speech in 5 Lines of Code

In this post, I will show you how to convert text to speech using Python. This will be a simple machine learning project that we will understand some basics of speech library called pyttsx3. If you are wondering where can we use text to speech conversion in real life, don’t worry you are not alone. I ask the same question before learning almost anything, I will ask myself how will this information help me, and where can I use it in real life. This way of thinking helps me to learn new things faster and motivates me to use that information in my personal life.

There are a couple of industries that I will list that uses text to speech in a very nice way. One of them is in a classroom environment, students who don’t know how to read yet but can understand when you speak to them. Technology is used very often in classrooms these days, and some text to speech software can help the teachers a lot. Instead of the teacher spending time with each kid, and reading for them, a simple text to speech can very helpful. Here is a nice video showing how text to speech is used in a classroom:

Another nice example of text to speech usage is on Chromebooks. They have an accessibility feature called “Select to Speak”, you can select/ highlight the text and the computer will read it aloud to the person using this feature.

The last example of text to speech conversion, I would like to share with you is audiobooks. Using some training data from previous audio files, it is possible to make a machine read a book using a trained person’s voice. Yes, artificial intelligence is becoming more and more part of our daily life. Also teaching a machine is much easier than teaching a human being. This makes the development of the field much faster. Let’s get back to the project.

We will be building a machine program that will convert our text to speech. The text can be in different languages, that’s when choosing our speech model, we have to make sure the languages are matching. Let’s start by importing the library.

Step 1 — Library

First, we will install the module so that we can use it in our program. The only module we will use in this project is Pyttsx3. It is text to speech module, one good thing about this module, it can work offline after you install it. Here is the code to install this module:

pip install pyttsx3

If you want to learn more about this module, here is the documentation. And now let’s import the module as a library to our program.

import pyttsx3

Step 2 — Text

This step is the easiest. You should define a variable and assign a text value to it. Your text can be something short or long, it depends on what you want. Below, you can see the text variable I’ve created, it is called test.

test = "Once upon a time, a person was trying to convert text to speech using python"

Step 3 – Speech Engine

In the following lines we are defining a variable to assign our speech engine. It is very simple.

engine = pyttsx3.init()

Now, we have to define the language we want our machine to speak. As mentioned earlier, it has to match with the language we used in our text. To see the avilable languages, run the following code:

voices = engine.getProperty('voices')
for voice in voices:
 print("Voice:")
 print(" - ID: %s" % voice.id)
 print(" - Name: %s" % voice.name)
 print(" - Languages: %s" % voice.languages)
 print(" - Gender: %s" % voice.gender)
 print(" - Age: %s" % voice.age)

You will something like this in your terminal.

Copy the ‘id’ of the language that you want to use, and let’s paste it into our program. We are using setProperty method to define the spoken language.

en_voice_id = "com.apple.speech.synthesis.voice.Alex"
engine.setProperty('voice', en_voice_id)

Final Step

We are almost done, the following code will tell our speech engine to say out loud the text we defined earlier. Basically, the computer will read it for you. Make sure your speakers are on to hear the sound.

engine.say(test)
engine.runAndWait()

Congrats, you have created a program that converts text to speech. Now, you have an idea of how those software work in the behind scenes. Hoping that you enjoyed this tutorial and learn something new today. Great tip: You can use this to communicate with your friends when you feel too lazy 🙂

Follow my blog and Towards Data Science to stay inspired.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s