pyttsx3
is a Python library that provides a simple way to convert text to speech. It is a popular text-to-speech (TTS) library that can be used to add speech synthesis capabilities to your Python programs. With pyttsx3
, you can convert text strings into spoken words, making it useful for applications such as voice assistants, accessibility features, and more.
Here’s a basic example of how to use pyttsx3
to convert text to speech:
- First, you need to install
pyttsx3
if you haven’t already. You can install it using pip:
pip install pyttsx3
- Once
pyttsx3
is installed, you can use it in your Python script:
import pyttsx3
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Set properties (optional)
engine.setProperty('rate', 150) # Speed of speech (words per minute)
engine.setProperty('volume', 1.0) # Volume (0.0 to 1.0)
# Convert and speak the text
text = "Hello, this is an example of text-to-speech using pyttsx3."
engine.say(text)
# Wait for the speech to finish
engine.runAndWait()
In this example, pyttsx3
is initialized with the init()
function, and you can customize properties such as the speech rate and volume using setProperty()
. Then, you use the say()
method to convert the text to speech, and the runAndWait()
method to wait for the speech to finish.
You can use pyttsx3
to build more complex applications with text-to-speech capabilities or integrate it into projects where speech output is needed.