top of page

Text to Speech in Python

Updated: Apr 11, 2022

How to convert text into speech in Python, Python-based program

What does this program do?

This program takes a piece of text and says it out loud, so someone can hear it.

Steps to Convert Text to Speech

Some people think that it is very difficult and time consuming to create a text to speech program. However, truth be told, text to speech is simple and a straight forward process.

On a high level, below are the list of steps to convert Text to Speech:

i. import modules

ii. Store text in a variable

iii. Use the package gtts to create sounds

iv. Save the sounds in a file

v. Play the file

Step #1: Install Packages

These instructions are meant for python 3.8, if you have an older version you can upgrade or try to follow along, but the code won’t be exactly the same.

Before you start writing python codes, let’s install some packages.

Go to the terminal and type “pip install” with the package name. An example is, “pip install gtts” and press “enter” key.

Similarly, install all the packages listed in the table below, in different lines in the terminal:

If any of these packages have already been installed on your computer, you will get the below message “Requirement already satisfied”:

If the package installation works, it will look like something like the below image “Successfully installed”:

If there is an error, there will be a red message like the below image “ERROR: No matching distribution”. Fix the error using the correct package name and successfully install all these packages.

Once all the above packages are successfully installed, it’s time to move on to importing the packages in the program and start coding.

Step #2: Import Packages

In this step you will import os and gtts, so you can use them in this program.

Step #3: Create the variable called text

In this step you will create a variable and call it text and you will store what you want the program to say in the variable called text.

Step #4: gTTS

In this step you will use the package gTTS to create sounds that say the text.

Step #5: Save the sound

In this step you save the sound in a .WAV file called “wavFile.wav”

Step #6: Playing the file

In this step you will use the operating system to play the wav file

Below is the completed source code

Thanks for staying till the end. If you enjoyed this article, please follow me for updates on new articles. Medium is where I want to give back to the community and help others learn coding. I welcome responses related to this article and its views. Please leave a response on what else you would like to learn and I’ll try to teach that at some point.

48 views0 comments


bottom of page