pyttsx3

2.99 · active · verified Sat Apr 11

pyttsx3 is a cross-platform, offline Text-to-Speech (TTS) library for Python 3. It allows developers to convert written text into spoken audio without an internet connection, leveraging native speech engines like Sapi5 on Windows, NSSpeechSynthesizer on macOS, and eSpeak/eSpeak-NG on Linux. The library provides a simple API to control speech rate, volume, select different voices, and save synthesized speech to audio files like MP3 or WAV. It is actively maintained, with regular updates to improve platform compatibility and fix bugs.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `pyttsx3` engine, adjust common properties like speech rate and volume, select a specific voice, queue multiple text utterances, and then process them using `runAndWait()`. It also shows how to stop the engine gracefully.

import pyttsx3

# Initialize the TTS engine
engine = pyttsx3.init()

# Get current speech rate and set a new one
rate = engine.getProperty('rate')
print(f"Current speaking rate: {rate}")
engine.setProperty('rate', 150) # words per minute

# Get current volume and set a new one (0.0 to 1.0)
volume = engine.getProperty('volume')
print(f"Current volume: {volume}")
engine.setProperty('volume', 0.9)

# Get available voices and set a female voice (if available)
voices = engine.getProperty('voices')
for voice in voices:
    if 'female' in voice.name.lower(): # Or check voice.id for specific IDs
        engine.setProperty('voice', voice.id)
        break

# Queue text to be spoken
engine.say("Hello, welcome to pyttsx3!")
engine.say("I can speak at different rates and volumes.")

# Process the speech queue and wait for it to finish
engine.runAndWait()

# Save speech to a file (requires espeak-ng on Linux to save to MP3/WAV, or other OS native capabilities)
# engine.save_to_file('Hello World', 'output.mp3')
# engine.runAndWait()

# Stop the engine when done
engine.stop()

view raw JSON →