pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it operates offline, making it versatile and eliminating the need for any API keys or internet connectivity. Its primary advantage is its ability to use native speech engines provided by the underlying operating system. On Windows, it leverages the SAPI5 TTS engine, while on macOS, it employs the NSSpeechSynthesizer API. Linux users benefit from the espeak backend.
The library's core workflow involves initializing an engine, adjusting its properties (like volume, rate, or voice), and invoking the say method to queue text for speech. The runAndWait command ensures the queued text is spoken before proceeding. Users can customize various aspects, such as selecting different available voices, adjusting the speech rate, and setting the desired volume. The underlying engine, based on the platform, translates the provided text into audible speech, offering an effective way for applications to communicate audibly with users.
Introduction:
The `pyttsx3` library provides a straightforward interface for converting text to speech in Python. It works offline and is compatible with both Python 2 and Python 3. This library uses different speech engines based on your operating system: -Windows:
Uses the built-in SAPI5 TTS engine.- Windows: Uses the built-in SAPI5 TTS engine. -
Mac:
Uses the built-in NSSpeechSynthesizer API.- Mac: Uses the built-in NSSpeechSynthesizer API. -
Linux:
Uses espeak.- Linux: Uses espeak. One of the significant advantages of `pyttsx3` is that the voices and properties related to the speech (like volume, rate) can be easily customized.
Configure:
Installation:
To get started, you need to install the `pyttsx3` library. You can do this using pip:pip install pyttsx3
sudo apt-get install espeak
1. Importing the Library:
import pyttsx3
2. Initializing the TTS Engine:
def initialize_engine():
engine = pyttsx3.init()
return engine
3. Setting Engine Properties:
def set_properties(engine, volume=0.9, rate=200, voice_id=None):
engine.setProperty("volume", volume)
engine.setProperty("rate", rate)
if voice_id:
engine.setProperty("voice", voice_id)
- `volume`: Loudness of the speech.
- `rate`: Speed of the speech (words per minute).
- `voice_id`: Specific voice to use for the speech. Default values are provided for these properties. If a voice ID is provided, it sets that specific voice.
4. Listing Available Voices:
def list_available_voices(engine):
voices = engine.getProperty("voices")
for voice in voices:
print("ID:", voice.id)
print("Name:", voice.name)
print("Languages:", voice.languages)
print("\n")
5. Converting Text to Speech:
def speak_text(engine, text):
engine.say(text)
engine.runAndWait()
6. Main Execution Block:
if __name__ == "__main__":
engine = initialize_engine()
print("Available Voices:\n")
list_available_voices(engine)
volume = float(input("Enter volume (0.0 to 1.0, default is 0.9): ") or 0.9)
rate = int(input("Enter speech rate (words per minute, default is 200): ") or 200)
voice_id = input("Enter voice ID (from the listed voices above or leave blank for default): ")
set_properties(engine, volume=volume, rate=rate, voice_id=voice_id or None)
text = input("Enter the text you want to convert to speech: ")
speak_text(engine, text)
- Initializes the TTS engine.
- Lists and prints available voices.
- Collects user inputs for volume, speech rate, and voice ID.
- Sets the engine properties based on user input.
- Collects the text from the user.
- Converts the user-provided text into speech.
Full Code:
import pyttsx3
def initialize_engine():
"""
Initialize the TTS engine.
Returns: initialized TTS engine.
"""
engine = pyttsx3.init()
return engine
def set_properties(engine, volume=0.9, rate=200, voice_id=None):
"""
Set properties for the TTS engine.
Args:
- engine: the TTS engine.
- volume: volume level for the speech, default is 0.9 (range is 0.0 to 1.0).
- rate: speech rate, words per minute. Default is 200.
- voice_id: if provided, sets the voice for the engine.
"""
engine.setProperty("volume", volume)
engine.setProperty("rate", rate)
if voice_id:
engine.setProperty("voice", voice_id)
def list_available_voices(engine):
"""
List all available voices and their details.
Args:
- engine: the TTS engine.
"""
voices = engine.getProperty("voices")
for voice in voices:
print("ID:", voice.id)
print("Name:", voice.name)
print("Languages:", voice.languages)
print("\n")
def speak_text(engine, text):
"""
Convert the given text to speech.
Args:
- engine: the TTS engine.
- text: the text to be converted to speech.
"""
engine.say(text)
engine.runAndWait()
if __name__ == "__main__":
# Initialize the TTS engine
engine = initialize_engine()
# List available voices
print("Available Voices:\n")
list_available_voices(engine)
# Configure properties
volume = float(input("Enter volume (0.0 to 1.0, default is 0.9): ") or 0.9)
rate = int(input("Enter speech rate (words per minute, default is 200): ") or 200)
voice_id = input("Enter voice ID (from the listed voices above or leave blank for default): ")
set_properties(engine, volume=volume, rate=rate, voice_id=voice_id or None)
# Convert text to speech
text = input("Enter the text you want to convert to speech: ")
speak_text(engine, text)