Getting Started with Python Text-to-Speech: A Beginner’s Guide to pyttsx3
If you’ve ever wanted to make your Python programs talk, pyttsx3
is a fantastic library that allows you to convert text to speech easily. This tutorial will walk you through the basics of using pyttsx3
to control the speaking rate, volume, and voice of the text-to-speech output. By the end of this guide, you will also know how to save the speech to an audio file. Let's get started!
Step 1: Install pyttsx3
Before you begin, you need to install the pyttsx3
library. You can do this using pip
, the Python package manager. Open your terminal or command prompt and run:
pip install pyttsx3
Step 2: Import and Initialize
To use pyttsx3
, you first need to import it and initialize the text-to-speech engine. Here’s how you do it:
import pyttsx3
engine = pyttsx3.init() # Object creation
Step 3: Adjusting the Speaking Rate
The speaking rate is how fast the text is spoken. By default, the rate might be too fast or too slow for your needs. You can check the current speaking rate and set a new one as follows:
# Getting the current speaking rate
rate = engine.getProperty('rate')
print(f'Current speaking rate: {rate}') # Print current voice rate
# Setting a new speaking rate
engine.setProperty('rate', 125) # New rate (try experimenting with different values)
In this example, we first get the current rate, which typically defaults to around 200 words per minute, and then set it to 125 words per minute for a slower and clearer speech.
Step 4: Adjusting the Volume
Volume control in pyttsx3
is straightforward. The volume level ranges from 0.0 to 1.0. Here’s how you can get the current volume and set it to the maximum:
# Getting the current volume level
volume = engine.getProperty('volume')
print(f'Current volume level: {volume}') # Print current volume level
# Setting a new volume level
engine.setProperty('volume', 1.0) # Max volume
Step 5: Changing the Voice
pyttsx3
comes with different voice options (usually male and female voices). You can choose which voice to use by listing the available voices and then setting your preferred one:
# Getting the list of available voices
voices = engine.getProperty('voices')
# Selecting a voice (0 for male, 1 for female, etc.)
engine.setProperty('voice', voices[1].id) # Change to female voice
In the code above, voices[1].id
refers to the second voice in the list, which is typically a female voice. You can change the index to select other voices available on your system.
Step 6: Making Your Program Speak
Now that you have set up the voice, rate, and volume, it’s time to make your program speak. Use the engine.say()
method to add the text you want to be spoken:
# Adding text to be spoken
engine.say("Hello World!")
engine.say(f'My current speaking rate is {rate}')
# Run the speech engine
engine.runAndWait()
The engine.runAndWait()
method processes the queued commands and makes the engine speak out the text.
Step 7: Saving Speech to an Audio File
pyttsx3
also allows you to save the spoken text to an audio file. This can be useful if you need to generate audio files for later use. Here’s how to save the speech to an MP3 file:
# Saving speech to an audio file
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()
In this example, the text “Hello World” is saved to test.mp3
in the current directory.
Complete Code Example
Here’s the complete code combining all the steps above:
import pyttsx3
# Initialize the engine
engine = pyttsx3.init()
# Adjust speaking rate
rate = engine.getProperty('rate')
print(f'Current speaking rate: {rate}')
engine.setProperty('rate', 125)
# Adjust volume
volume = engine.getProperty('volume')
print(f'Current volume level: {volume}')
engine.setProperty('volume', 1.0)
# Change voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) # Selecting a female voice
# Make the engine speak
engine.say("Hello World!")
engine.say(f'My current speaking rate is {rate}')
engine.runAndWait()
# Save to a file
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()
# Stop the engine
engine.stop()
Conclusion
With these steps, you can easily incorporate text-to-speech functionality into your Python applications using pyttsx3
. Experiment with different rates, volumes, and voices to see what works best for your needs. Whether you're developing an accessibility tool or just having fun with speech synthesis, pyttsx3
provides a simple and powerful way to make your Python programs speak.
Follow for More Tutorials!
Instagram:-
https://www.instagram.com/techstorieswithayushmaan?igsh=aGQ0OWplOHJjNHR6
Youtube:-
https://youtube.com/@techstorieswithayushmaan?si=ZOwYeO-YFALbwJ-A
LinkedIN:-
https://www.linkedin.com/in/srivastavayushmaan