Text-to-Speech in Python: A Complete Guide to Using pyttsx3 with Code Examples

MoneyTech360
By -
0
Text-to-Speech in Python: A Complete Guide to Using pyttsx3 with Code Examples

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
For Linux, you might need to ensure you have `espeak` backend installed:
sudo apt-get install espeak

1. Importing the Library:

   import pyttsx3
This line imports the `pyttsx3` library, which provides functions to convert text into speech.

2. Initializing the TTS Engine:

   def initialize_engine():
       engine = pyttsx3.init()
       return engine
The function `initialize_engine` initializes the text-to-speech engine and returns it. The initialization is done using `pyttsx3.init()`.

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)
The `set_properties` function sets three properties for the TTS engine:
- `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")
This function retrieves a list of available voices using `engine.getProperty("voices")` and then iteratively prints out the ID, name, and languages of each voice.

5. Converting Text to Speech:

   def speak_text(engine, text):
       engine.say(text)
       engine.runAndWait()
The `speak_text` function takes the engine and a text string as inputs. The `engine.say(text)` queues the text for speech, and `engine.runAndWait()` ensures that the speech is completed before moving on.

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)
This block runs if the script is executed directly (not imported as a module). It performs the following steps:
- 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)


Your feedback and suggestion is very valuable for us.
So please comment your thoughts and request code below



Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!