📅  最后修改于: 2023-12-03 14:46:48.734000             🧑  作者: Mango
pyttsx3 is a cross-platform and multi-engine text-to-speech (TTS) library for Python. It works with both Python 2 and Python 3 versions. This library provides a simple way to add text-to-speech functionality to your Python applications using a variety of voice engines.
To install pyttsx3, you can use pip:
pip install pyttsx3
Here is a simple example demonstrating how to use pyttsx3 to convert text to speech:
import pyttsx3
# Initialize the TTS engine
engine = pyttsx3.init()
# Convert text to speech
engine.say("Hello, world!")
# Play the speech
engine.runAndWait()
In this example, we first initialize the TTS engine using the init()
method. We then use the say()
method to convert the text "Hello, world!" to speech. Finally, we use the runAndWait()
method to play the speech.
pyttsx3 also provides several methods for customizing the TTS engine:
setProperty(name, value)
: This method allows you to set various properties of the TTS engine such as voice, speed, and volume.# Set the voice
engine.setProperty('voice', 'com.apple.speech.synthesis.voice.Alex')
# Set the speech speed
engine.setProperty('rate', 150)
# Set the speech volume
engine.setProperty('volume', 0.5)
getProperties()
: This method returns a dictionary containing all the properties of the TTS engine.# Get all the properties of the engine
properties = engine.getProperties()
# Print the properties
print(properties)
getVoices()
: This method returns a list of all the available voices for the TTS engine.# Get all the available voices
voices = engine.getVoices()
# Print the voices
for voice in voices:
print(voice.id)
pyttsx3 is a powerful and flexible text-to-speech library for Python. It provides a simple way to add TTS functionality to your Python applications using a variety of voice engines. With its cross-platform support and customizable features, pyttsx3 is a great choice for any Python developer looking to add speech functionality to their projects.