Photo from morioh.com

Text to Speech in Python

Md Monir Hossain Showrav
Analytics Vidhya

--

This article will explain the step by step procedure of building a python script which will convert text to speech.

Prerequisite:

You will be required to install Python 3.6 or above on your computer. And you need to download a python library —

1. gTTS — This is a Python library and CLI tool to interface with Google Translate’s text-to-speech API.

Installation:

Considering you’ve already installed Python 3.6 or above on your computer(if not you can see this article). In this part, I’m going to explain only the installation of gTTS.

I am only explain the installation process for windows:

On windows is easy to install gTTS. On windows, you can install it from the PyPI using pip if you have pip installed

pip install gTTS
installation process gTTs

Getting Started:

At first, you have to import the necessary packages. For this you need to import two packages

from gtts import gTTS
import os

Now you need to store the text those you want to convert text from speech and also you need to select the language

tts = gTTS(text='Anything you want', lang='en')

After that you need to store the speech file. You can save file in any name in your computer. The file always save in mp3 format

tts.save("file_name.mp3")
os.system("file_name.mp3")

the enter code structure in like this —

from gtts import gTTS
import os
tts = gTTS(text='Anything you want', lang='en')
tts.save("file_name.mp3")
os.system("good.mp3")
text to speech

That’s all, now you can just run the python file, then you see a mp3 file in your computer. And that is the audio file of your text.

Thank you..

You can found the script in my github also click here

About the Author:

Md Monir Hossain Showrav is a Data Science Enthusiast. I am doing my Bachelor of Computer Science and Engineering at the International University of Business Agriculture and Technology in the Department of Computer Science and Engineering. Currently, I am in the 8th Semester.

You can find me on Facebook, LinkedIn, Github.

--

--