Skip to content
Home » How to Convert Text to Speech in Python

How to Convert Text to Speech in Python

In this tutorial let’s learn about How to Convert Text to Speech in Python. We will not be developing neural networks and training the model to produce outcomes in this lesson because it is too sophisticated and difficult. Instead, we’ll use several APIs and engines that provide it. There are numerous APIs available that provide this service; one of the most often used is Google Text to Speech.

Initially, to proceed further do the basic installations that are required in converting text to speech. Run the below command to install packages.

pip3 install gTTS pyttsx3 playsound

Convert Text to Speech using gTTS Module in Python

Google Text-To-Speech is an API that Google uses to read text and generate audio output. This API is connected with a Python module called gtts, which can convert text to voice, do audio modification, and even save the final product by saving audio in a byte-sized object.

The gTTS function will be used to generate an object that will read the text and convert it to an audio object. With this function, we can use a variety of parameters. Using the slow parameter, we can slow down the output. The Google API supports a variety of languages, which we may specify using the lang option.

from gtts import gTTS
from playsound import playsound

x = gTTS("Welcome to Similar Geeks")
x.save('file.mp3')
playsound('file.mp3')

In the above code we are saving the text into an mp3 file and using the playsound module to play the mp3 file.

Convert Text to Speech using pyttsx3 Module in Python

pyttsx3 is a Python text-to-speech conversion library. It operates offline, unlike other libraries, and is compatible with Python 2 and 3. To obtain a reference to a pyttsx3. Engine instance, an application calls the pyttsx3.init() factory method. It is a simple tool that turns the entered text into speech.

The pyttsx3 module supports two voices, one female and one male, which are provided by “sapi5” for Windows.

import pyttsx3  
x = pyttsx3.init()  
data = "Welcome to Similar Geeks"  
x.say(data)  
x.runAndWait()

In the above code the say() function then adds the relevant text to the queue to be said. The runAndWait() function is then used to play the command from the queue.

Convert Text to Speech using win32com.client in Python

The win32com library includes a number of exciting methods, one of which being the library’s Dispatch method. When the SAPI argument is supplied, the dispatch method is invoked. SpVoice It uses the Microsoft Speech SDK to pronounce what you type on the keyboard.

You can install win32com.client module using:

pip install pypiwin32

Example:


import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

s = 'Welcome to Similar Geeks'
speaker.Speak(s)

Conclusion

In this article we discussed about the different module like win32com.client, pyttsx3  and gTTS  which are used to convert text to speech using python. Also gone through the code samples for better understanding.

Also Read:

Python | How to Play Mp3 File using Python
Free Python Books List and Download Links
How to Remove Character From String Python