Text To Speech using Python

In this Article, We will learn how to convert Text to Speech using Python Google Text to Speech API. Before this, I have already posted article and video about how to convert speech to text using python.

Here is The Detailed Video Explanation of Text to Speech Conversion using Python

Google Text to Speech API

Now Coming Towards our Topic, That is Text to Speech using Python. There are so many APIs and modules available in Python to convert text to speech.

But We will be using Google Text to Speech API as it’s Accuracy is greater then any other module. and It is also very easy to use. We can easily Convert our text to Audio Speech and can download that audio as mp3 in a very few lines of code.

Text to Speech using Python

Installation

Libraries required for Converting Text to Speech is gTTS – Google Text to Speech
Open Your Anaconda CMD or Terminal Depending Upon Which Environment you are working with. In this article I am following Jupyter Notebook so I will be using Anaconda Prompt to Install required library.

Run Command

pip install gTTS

Coding

Once Required Library is Installed. Open Jupyter Notebook. Create New Python 3 File. And Write this Code:

from gtts import gTTS
mytext = "Welcome to All Programmers"
language ='en'
myaud = gTTS(text = mytext, lang = language, slow = True)
myaud.save('myaudio.mp3')

gTTS accepts parameters, Text that we want to convert to speech. lang parameter that needs to be a 2 character language code specifying the language of text. and Slow Parameter that will be either true or false. Slow Parameter Specifies the Speed of Audio Speech. save function accepts 1 parameter and that is path and name of the audio file to save it in the directory. Here I have given only name So it will save the audio in the working directory where my code file is saved. If I want to save it in any other directory e.g D:/MyFiles/ then I will give path as r’D:/MyFiles/myaudio.mp3′ .

Execute the program and it will save the Audio of given text in specified Directory.

If you want to learn more about text to speech using python gTTS API visit official documentation gTTS · PyPI

If you have any questions or facing any error. Let me know in Comments Section Thank You.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *