Python: Convert Text to Speech [Beginner’s Guide]

Text-to-speech conversion is a technique used to generate a voice output based on a text.

This might be useful when you don’t want to read a document but want to listen to it instead. Also, some more advanced text-to-speech tools can be used to create a realistic voice for videos, ads, or podcasts.

This guide teaches you how to use Python to convert text to speech. After reading this guide, you have the knowledge to build a small text-to-speech converter.

🎙️By the way, if you are looking for a professional tool to create a realistic voice from text, read Best AI Voice Generators.

And if you are looking for a quick answer, first install gTTS module with:

pip install gTTS

And then create a Python script like this:

from gtts import gTTS
import os

mytext = "Hi, this is an example of converting text to audio. This is a bot speaking here, not a real human!"
audio = gTTS(text=mytext, lang="en", slow=False)

audio.save("example.mp3")
os.system("start example.mp3")

When you run this script, it generates a new mp3 file and plays it. The file is located in the same folder as your script.

Below you find a step-by-step guide on how this code works.

How to Convert Text to Speech in Python

As you might imagine, there is a whole bunch of text-to-speech tools available for Python. The one we are going to use in this guide is called Google Text-to-Speech API (gTTS API for short).

Text to speech in Python

The gTTS API is easy to use. You can simply feed a text document to it and get an mp3 file out with the spoken version of the text.

To use the gTTS API to convert text into voice in Python:

  1. Install gTTS on your system.
  2. Import gTTS to your program.
  3. Specify a piece of text to convert to audio.
  4. Pass the text into the gTTS engine and specify the language and speed.
  5. Save the file.
  6. Open the file and listen to it.

Let’s take a step-by-step overview of this process.

1. Install gTTS to Your System

Before you can use the text-to-speech converter in Python, you need to install the gTTS module on your system.

You can use pip to install it. Open up the command line tools and run the following command:

pip install gTTS

2. Import gTTS to Your Program

Once you have installed the gTTS module in your system, you can import it to your code project.

By the way, to use the program to play the mp3 file, you also need to import the built-in os module.

So add these two lines to the beginning of your Python file:

from gtts import gTTS
import os

3. Specify a Piece of Text to Be Converted

Now you have the necessary tools in the code file and you are ready to convert text to speech.

Next, specify a piece of text you want to convert to speech.

For example:

mytext = "Hi, this is an example of converting text to audio. This is a bot speaking here, not a real human!"

4. Pass the Text into gTTS Engine

Now, let’s input the text to the gTTS engine and specify the language as English:

audio = gTTS(text=mytext, lang="en", slow=False)

5. Save the File

Now that you have the audio object specified, let’s export it to an mp3 file:

audio.save("example.mp3")

This saves the audio file to the same folder where your program file lives. Now you can open up the file to listen to it.

6. Listen and Enjoy the File

If you want to use your Python program to play the file, you need to call the os.system() function.

Here is how to do it:

os.system("start example.mp3")

This is the last line of your program. If you now run the file, you hear the message being spoken by your machine. In addition, it generates an audio file as an output.

Full Code

For your convenience, here is the full code of the step-by-step guide:

from gtts import gTTS
import os

mytext = "Hi, this is an example of converting text to audio. This is a bot speaking here, not a real human!"
audio = gTTS(text=mytext, lang="en", slow=False)

audio.save("example.mp3")
os.system("start example.mp3")

Wrap Up

Today you learned how to use the gTTS module to convert text to speech in your Python program.

To recap, all you need to do is install gTTS module to your system and input it some text in your Python program.

Thanks for reading. Happy coding!

You May Also Like to Read