Python pyttsx3 module
Python pyttsx3 Module
1. Introduction
pyttsx3 is a text-to-speech library that converts text to speech using Python code. It runs on different operating systems and provides customizable options such as volume, speech rate, and voice engine selection. In this article, we will introduce pyttsx3’s usage and common features in detail.
2. Installation
First, we need to install the pyttsx3 module. You can install it using pip:
pip install pyttsx3
3. Converting Text to Speech
Let’s take a look at a simple example to convert a text to speech:
import pyttsx3
def text_to_speech(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
text = "Hello, World!"
text_to_speech(text)
Code Explanation:
– Imports the pyttsx3 module.
– Defines a function named text_to_speech
that accepts a text parameter, text
.
– Initialize a pyttsx3
engine.
– Use the engine’s say
method to convert text to speech.
– Use the engine’s runAndWait
method to start the engine and wait for the speech playback to complete.
– Define a string variable text
and pass it to the text_to_speech
function.
Run the above code and you will hear the sentence “Hello, World!” converted to speech output.
4. Setting the Speech Rate and Volume
pyttsx3 allows you to customize the audio output by adjusting the speech rate and volume. These properties can be set using the engine.setProperty
method. Let’s look at an example:
import pyttsx3
def text_to_speech(text, rate, volume):
engine = pyttsx3.init()
engine.setProperty('rate', rate) # Set the speaking rate
engine.setProperty('volume', volume) # Set the volume
engine.say(text)
engine.runAndWait()
text = "Hello, World!"
rate = 150 # Set the speaking rate to 150
volume = 0.9 # Set the volume to 0.9
text_to_speech(text, rate, volume)
Code Explanation:
– Added two parameters, rate
and volume
, to the text_to_speech
function to set the speaking rate and volume.
– Use the engine.setProperty
method to set the speech rate and volume properties.
– Define the string variable text
, the integer variable rate
, and the floating-point variable volume
and pass them to the text_to_speech
function.
Run the above code and you will hear the sentence “Hello, World!” converted to speech output at the set speech rate and volume.
5. Selecting a Speech Engine
pyttsx3 supports multiple speech engines. By default, it uses the system default speech engine. You can select the desired speech engine using the name
parameter of the pyttsx3.init()
method. Let’s take a look at an example:
import pyttsx3
def text_to_speech(text, engine_name):
engine = pyttsx3.init(driverName=engine_name) # Specify the desired speech engine
engine.say(text)
engine.runAndWait()
text = "Hello, World!"
engine_name = "sapi5" # Use the sapi5 engine
text_to_speech(text, engine_name)
Code Explanation:
– Adds a parameter, engine_name
, to the text_to_speech
function to select the speech engine.
– Uses the pyttsx3.init(driverName=engine_name)
method to initialize the specified speech engine.
– The string variables text
and engine_name
are defined and passed to the text_to_speech
function.
Running the above code will result in the sentence “Hello, World!” being converted to speech using the SAPI5 engine. You can try using other speech engines, such as “espeak” or “nsss.”
6. Saving Speech Output to an Audio File
Pyttsx3 can also save the speech output of text-to-speech conversion to an audio file. Use the engine.save_to_file
method to specify the output file name and file format. Let’s look at an example:
import pyttsx3
def text_to_speech(text, file_name):
engine = pyttsx3.init()
engine.save_to_file(text, file_name)
engine.runAndWait()
text = "Hello, World!"
file_name = "output.wav" # Output file name is output.wav
text_to_speech(text, file_name)
Code Explanation:
– Adds a parameter, file_name
, to the text_to_speech
function to specify the output audio file name.
– Uses the engine.save_to_file
method to save the converted speech output to the specified file name.
– The string variables text
and file_name
are defined and passed to the text_to_speech
function.
Running the above code will generate an audio file named “output.wav” containing the spoken output of “Hello, World!”.
7. Summary
In this article, we learned how to use the pyttsx3 module to convert text to speech and apply common features such as adjusting speech rate and volume, selecting a speech engine, and saving the speech output as an audio file. pyttsx3 provides many other features, such as changing speech properties and using different languages. You can refer to the official documentation to learn more about pyttsx3’s features and usage to meet your needs.