DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on • Updated on

Python Text to Speech (TTS) Using Pyttsx3

Voice Assistant is amazing, isn’t it? Guess what, we can create our own voice assistant to support us in our everyday lives. It would not be high-end voice assistance but a combination of basic text-to-speech and speech recognition in Python.

Installing Pyttsx3 using pip

pip install pyttsx3
Enter fullscreen mode Exit fullscreen mode

This will install the pyttsx3 library in your machine or the virtual environment if you are using it.

Text-To-Speech using Pyttsx3 in Python

We have installed the necessary package needed for Text-To-Speech Conversion, so now lets code it.

Text-To-Speech Python

import pyttsx3

engine = pyttsx3.init()
engine.say("This is Text-To-Speech Engine Pyttsx3")
engine.runAndWait()
engine.stop()
Lets understand this code line by line.
Enter fullscreen mode Exit fullscreen mode

First of all, we‘re going to import the pyttsx3 package that we’ve installed using the pip. The name pyttsx3 is really big and we might make an error when coding a big program.

We can give a short name using the Python import alias syntax.

Example:

Python Import Alias

pip install pyttsx3 as tts
Enter fullscreen mode Exit fullscreen mode

You can use a different name as you like. I’m going for the original name because it specifically describes the name of the package. If a shorter syntax for this package is created later, you can use it.

Pyttsx3 Engine Initialization

engine = pyttsx3.init()
Enter fullscreen mode Exit fullscreen mode

The above code initializes the pyttsx3 package. The Instance of the initialized pyttsx3 package is stored in the engine variable. We are calling the variable engine as it works as the engine and converts Text-To-Speech whenever execute the functions from the package.

Say Function in pyttsx3

engine.say("This is Text-To-Speech Engine Pyttsx3")
Enter fullscreen mode Exit fullscreen mode

There is a built-in say() function in the pyttsx3 package that takes a string value and speaks it out.

runAndWait Function
engine.runAndWait()
Enter fullscreen mode Exit fullscreen mode

This function keeps track when the engine starts converting text to speech and waits for that much time, and do not allow the engine to close. If we don’t write this code, it may happen that the engine might not work properly as the processes will not be synchronized.

After all the processes are over, we shut down the engine by calling the stop() function.

Change Text-to-Speech Voice In Pyttsx3 Python

We can also change the voice of the engine, default is the voice of a male named David.

To change the voice of the pyttsx3 engine, first, we will have to get the list of objects of voices.

##Pyttsx3 getProperty
voices = engine.getProperty('voices')
Enter fullscreen mode Exit fullscreen mode

The getProperty() function of the pyttsx3 package takes a string as a parameter and returns an object matching the string.

Read more about Text to Speech in Python. Read more about Text-to-Speech.

Top comments (0)