How to Convert Text to Speech in Python

Manav Narula Feb 02, 2024
  1. Use the gTTS Module to Convert Text to Speech in Python
  2. Use the pyttsx3 Module to Convert Text to Speech in Python
How to Convert Text to Speech in Python

Due to the advancement in technology over the past few decades, we have many devices equipped with speech recognition. Python has a wide variety of modules available which can work with audio objects. Some modules can convert some written text to audio.

In this tutorial, we will discuss how to convert text to speech using Python.

Use the gTTS Module to Convert Text to Speech in Python

Google created an API called Google Text-To-Speech which they use to read a text and give audio output. This API is integrated with a Python module called gtts, which can convert text to speech, perform audio manipulation, among other things, by storing audio in a byte-sized object, and even save the final output.

We will use the gTTS function to create an object which will read the text and convert it to an audio object. We can use many parameters with this function. We can reduce the speed of the output using the slow argument. The Google API supports different languages, and we can mention supported languages using the lang parameter.

We will implement this method in the following code.

from gtts import gTTS
from playsound import playsound

s = gTTS("Sample Text")"sample.mp3")

In the above code, we create an object which reads the desired text. We save this object containing the speech audio using the save() function and then play it using the playsound() function. The playsound() function imported from the playsound library can play an audio file.

Note that given this module uses the Google API to convert text to speech, it will require an active internet connection. You can consider the method discussed below if you want it to run offline.

Use the pyttsx3 Module to Convert Text to Speech in Python

The pyttsx3 is another module that can perform such conversions and work without an internet connection. First, we will create an object which references the pyttsx3.Engine using the init() constructor from this module. Then the say() function here adds the required text to be spoken in the queue. Then we use the runAndWait() function to play the command from the queue.

For example,

import pyttsx3

s = pyttsx3.init()
data = "Sample Text"

We can set the properties like the playback rate of the final output using the setProperty() method. This module is compatible with Python 2 and Python 3.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.


Related Article - Python Audio