Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Text To Speech Conversion Using Python

2 min read

Python is an incredibly strong and flexible language used for multi-purpose programming. There are a lot of things possible using Python one of them is Text to Speech conversion.

In this tutorial, we will go through the conversion of text to speech using Python and Google's Text-To-Speech service. Google Cloud Text-to-Speech converts text into human-like speech in more than 100 voices across 20+ languages and variants.

It applies groundbreaking research in speech synthesis (WaveNet) and Google's powerful neural networks to deliver high-fidelity audio. With this easy-to-use API, you can create lifelike interactions with your users that transform customer service, device interaction, and other applications.

We are going to use a Python package called gTTS (Google Text-to-Speech) for this purpose.

gTTS (Google Text-to-Speech), is a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Which writes spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout.

It features flexible pre-processing and tokenizing, as well as automatic retrieval of supported languages. The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more.

Let's being with installing the gTTS package first.

pip install gtts

After the installation is done you can go ahead run the following Scripts.

Note that gTTS which works perfectly in python but it needs an internet connection to work since it relies on Google to get the audio data so your machine must have a working internet connection.

Python Program to convert a text into speech

import os
from gtts import gTTS

Text = "I love Django central and Python programming Language"

print("please wait...processing")
TTS = gTTS(text=Text, lang='en-uk')

# Save to mp3 in current"voice.mp3")
# Plays the mp3 using the default app on your system
# that is linked to mp3s.

os.system("start voice.mp3")

Save the file and run it this should open your default music player app and play the audio.

But what if you wanted to convert a large string or document into speech for that you might wanna save the content into a text file then convert it into speech as follows.

Python program to Convert a text file into speech

import os
from gtts import gTTS

# You will need a text file named test.txt
# to play, put it in the current dir.

FLIST = open("test.txt", "r").read().replace("\n", " ")
print("please wait...processing")
TTS = gTTS(text=str(FLIST), lang='en-uk')

# Save to mp3 in current"voice.mp3")
# Plays the mp3 using the default app on your system# that is linked to mp3s.
os.system("start voice.mp3")

Save and run the file you should get the desired output.


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read