Exploring Text-to-Speech in Python with pyttsx3: A Comprehensive Guide

Ayushmaan Srivastav
3 min readOct 25, 2023

--

Introduction

In today’s tech-driven world, the ability to make your applications more accessible and user-friendly is essential. One way to achieve this is by adding text-to-speech functionality. Python, a versatile and popular programming language, provides an excellent module for this purpose: pyttsx3. In this blog, we’ll delve into the pyttsx3 module, explore its various functionalities, and provide code examples to help you get started with text-to-speech applications.

What is pyttsx3?

pyttsx3 is a Python library that enables text-to-speech conversion. It is a more advanced and feature-rich version of the older pyttsx library, making it an excellent choice for developing text-to-speech applications in Python. With pyttsx3, you can convert text to speech and customize the speech settings according to your needs.

pip install pyttsx3

Basic Text-to-Speech

Let’s start by exploring how to convert text to speech using pyttsx3. It’s as simple as initializing the engine, providing the text, and letting the engine do the rest.

import pyttsx3

# Initialize the text-to-speech engine
engine = pyttsx3.init()

# Provide the text you want to convert to speech
text = “Hello, welcome to the world of pyttsx3.”

# Convert text to speech
engine.say(text)

# Play the converted speech
engine.runAndWait()

Changing the Rate and Volume

You can customize the speech by adjusting the rate and volume. The rate determines how fast or slow the speech is, and the volume controls the loudness.

import pyttsx3

engine = pyttsx3.init()

text = “Let’s adjust the rate and volume of the speech.”

# Set the rate (words per minute)
engine.setProperty(‘rate’, 150) # You can adjust this value as needed

# Set the volume (0.0 to 1.0)
engine.setProperty(‘volume’, 1.0) # 1.0 is maximum volume

engine.say(text)
engine.runAndWait()

Changing the Voice

Pyttsx3 allows you to change the voice used for speech. You can get a list of available voices and select the one that suits your application.

import pyttsx3

engine = pyttsx3.init()

# Get the list of available voices
voices = engine.getProperty(‘voices’)

# Select a voice
engine.setProperty(‘voice’, voices[1].id) # You can change the index as needed

text = “Changing the voice for text-to-speech.”

engine.say(text)
engine.runAndWait()

Saving Speech to a File

You can save the generated speech to an audio file for later use. Here’s how you can do it:

import pyttsx3

engine = pyttsx3.init()

text = “This speech will be saved to a file.”

engine.save_to_file(text, “output.mp3”)
engine.runAndWait()

Events and Callbacks

Pyttsx3 supports events and callbacks, which allow you to control the behavior of the text-to-speech engine more flexibly. For example, you can add callbacks to perform actions when speech starts, ends, or is interrupted.

import pyttsx3

engine = pyttsx3.init()

def onStart(name):
print(“Speech started for: “ + name)

def onEnd(name, completed):
print(“Speech ended for: “ + name + “, completed: “ + str(completed))

engine.connect(‘started-utterance’, onStart)
engine.connect(‘finished-utterance’, onEnd)

text = “Using events and callbacks in pyttsx3.”

engine.say(text)
engine.runAndWait()

Conclusion

In this blog, we’ve explored the pyttsx3 module and its various functionalities for text-to-speech conversion in Python. Whether you want to create more accessible applications, develop voice assistants, or add speech output to your projects, pyttsx3 provides a versatile and easy-to-use solution. By leveraging pyttsx3’s capabilities to adjust rate, volume, voice, and even save speech to a file, you can create rich and engaging user experiences.

Start experimenting with pyttsx3 today and enhance the accessibility and user-friendliness of your Python applications. The possibilities are endless, and pyttsx3 makes it accessible to developers of all skill levels.

Happy coding!

--

--

No responses yet