How to Text-To-Speech using Python and gTTS

How to Text-To-Speech using Python and gTTS

Creating Text-To-Speech files has been a dream for many of us since we were kids. Now, with Python, those dreams can become true with few lines. Let's learn about - Creating Text-To-Speech using Python and gTTS

Creating Text-to-speech files has been a dream for many of us since we were kids. Now, with Python, those dreams can become true with few lines. Let's see how!

On this lesson you'll learn how to:

  • Create an mp3 from a string of text
  • Ask the user for a text and create an mp3
  • Ask the user for a text file, extract the text and create an mp3
  • How to play mp3 with Python.


This lesson is best watched than read, as you can listen to the files I create:

But if you can't (or don't want to) watch a video, you can do it too without a problem.

First, create an environment. I have used pipenv but you can use your virtual environment of choice.

Then, we only need to install gTTS with 'pip install gtts'.

Once you have created your virtual environment and installed gTTS, you are set. We are only going to use this and the Python core libraries.

Ah, and don't forget to create a text file with the .txt extension

The code

To be honest, the code is pretty straight-forward, as the gTTS library does all the heavy lifting, so I'm going to give you blocks of code and a brief explanation.

First, create a file and import two Python libraries and set our options:

import os

from gtts import gTTS


text_to_read = "This is just a test using GTTS, a Python package library" language = 'en' slow_audio_speed = False filename = 'my_file.mp3'

Reading from a string

Now, we create the first of our functions that will read the text from text_to_read, with the language voice and a normal speed, as _slow_audio_speed _is false.

 Reading from a string

def reading_from_string(): audio_created = gTTS(text=text_to_read, lang=language, slow=slow_audio_speed)

os.system(f'start {filename}')

We create a gTTS object with the options we created at the start, we save it to the filename _(that's it, my_file.mp3). Now we are done, but we want to play the file we have just created. So, we use the _os library to play the file with the name filename on the current folder.

Reading from the user's input

 Reading from user input

def reading_from_user(): user_input = input("What text should I read for you?n")

audio_created = gTTS(text=user_input, lang=language, slow=slow_audio_speed)

os.system(f'start {filename}')

Pretty much the same as before. With only one difference: Now we are asking the user to introduce some text to transform it into an audio file.

Reading from a file

def reading_from_file():
    file_to_read = input("Please, insert the name of a file to read:n") + '.txt'
    f = open(file_to_read, 'r')
    file_text =

audio_created = gTTS(text=file_text, lang=language, slow=slow_audio_speed)

os.system(f'start {filename}')

This is the most complex function yet still pretty easy to understand. We ask the user to introduce the name of a file, we add the .txt extension, we open and read the text, and as always we create the mp3.

Running the script

We only need to declare which function we will use at the end of the code:

if name == 'main':

You can easily switch the function called.

Or don't set any function and run the python interpreter, and keep asking for functions to run with "python –i"


I found the gTTS library this weekend and I played around with it and I had a lot of fun, and I wanted to share that with you. The library goes deeper, so here are the docs if you want to learn more: gTTS docs.

I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

Thanks for reading. Keep Visiting

Learning Python: From Zero to Hero

Python Basics: Functions

☞ Learn Python by Building a Blockchain & Cryptocurrency

☞ Python and Django Full Stack Web Developer Bootcamp

☞ The Python Bible™ | Everything You Need to Program in Python

☞ Learning Python for Data Analysis and Visualization

Originally published on

python web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[]( "")**, our full-stack Python development services...

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[]( "")**, we constantly endeavor to give you exactly what you need. If you need to...

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Hire Web Developer

Looking for an attractive & user-friendly web developer?, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...