How to convert text to speech with Python

Manpreet Singh
2 min readJul 7, 2021

Welcome back! Converting text to speech is one of the coolest things you can do with Python, luckily for us there are tons of packages that allow us to do this! For this tutorial we will take a look at one specific package: gtts. As long as you have Python installed on your machine, access to pip and an IDE installed you are more than ready to get started with this project, let’s get to coding!

The Code

Starting off, we want to install the gtts package, in order to do this we will use the following pip command:

pip install gtts

Awesome! At this point we want to make our way to our IDE, we will then need to import the gtts package and import os as well, we do so with the following line:

from gtts import gTTS
import os

At this point we want to setup a string variable, the variable name can be anything and the text can be anything as well:

thetext = ‘this is a test!’

We will then have to create a language variable with your specific language:

language = ‘en’

Awesome! At this point we will create a another variable with our gTTS command, this specific variable will bring in our thetext variable and our language variable from above:

--

--