Ricky Martin

Ricky Martin


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 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,

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 = f.read()

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 NAME_FILE.py”


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 letslearnabout.net

#python #web-development

What is GEEK

Buddha Community

How to Text-To-Speech using Python and gTTS
Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Alec  Nikolaus

Alec Nikolaus


Convert Text to Speech in Python

Learn how to convert your Text into Voice with Python and Google APIs

Text to speech is a process to convert any text into voice. Text to speech project takes words on digital devices and convert them into audio with a button click or finger touch. Text to speech python project is very helpful for people who are struggling with reading.

Project Prerequisites

To implement this project, we will use the basic concepts of Python, Tkinter, gTTS, and playsound libraries.

  • Tkinter is a standard GUI Python library that is one of the fastest and easiest ways to build GUI applications using Tkinter.
  • gTTS (Google Text-to-Speech) is a Python library, which is a very easy library that converts the text into audio.
  • The playsound module is used to play audio files. With this module, we can play a sound file with a single line of code.

To install the required libraries, you can use pip install command:

pip install tkinter
pip install gTTS
pip install playsound

Download Python Text to Speech Project Code

Please download the source code of Text to Speech Project: Python Text to Speech

Text to Speech Python Project

The objective of this project is to convert the text into voice with the click of a button. This project will be developed using Tkinter, gTTs, and playsound library.

In this project, we add a message which we want to convert into voice and click on play button to play the voice of that text message.

  • Importing the modules
  • Create the display window
  • Define functions

So these are the basic steps that we will do in this Python project. Let’s start.

#python tutorials #python project #python project for beginners #python text to speech #text to speech convertor #python

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Vincent Lab

Vincent Lab


Text to Speech in Node.js

In this video, I will be showing you how to turn text into speech in Node.js

#javascript #text to speech #javascript api #text to speech app #node.js text to speech #javascript text to speech

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…

You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.

1. init

class AnyClass:
    def __init__():
        print("Init called on its own")
obj = AnyClass()

The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

Init called on its own

2. add

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

class AnyClass:
    def __init__(self, var):
        self.some_var = var
    def __add__(self, other_obj):
        print("Calling the add method")
        return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2

#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python