Build a Python Text-to-Speech Converter Using pyttsx3

 Text-to-Speech (TTS) technology allows computers to convert written text into spoken words. It’s widely used in assistive technologies, audiobooks, virtual assistants, and voice-enabled applications.

In this blog, we’ll build a simple Text-to-Speech converter in Python using the pyttsx3 library.

 


What is a Text-to-Speech Converter?

A TTS converter takes text input and generates speech output using synthetic voices. It is commonly used in:

Reading Assistants – Helping visually impaired users.
Audiobooks – Converting e-books into audio.
Chatbots & AI Assistants – Enhancing voice-based interactions.
Language Learning – Improving pronunciation and comprehension.

 
 


Features of Our Python TTS Converter

Offline Support – Works without an internet connection.
Multiple Voice Options – Choose between male and female voices.
Adjustable Speech Rate – Control the speed of the voice.
Simple & Lightweight – Requires minimal dependencies.

 


Step-by-Step Guide to Building a TTS Converter in Python

Step 1: Install Required Library

We’ll use the pyttsx3 library for text-to-speech conversion. Install it using:

pip install pyttsx3

Step 2: Write the Python Code

Below is the Python script for the TTS Converter:

import pyttsx3

def text_to_speech(text):
    engine = pyttsx3.init()  # Initialize the TTS engine
    engine.setProperty('rate', 150)  # Adjust speech speed (default is 200)
    
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)  # Change voice (0 for male, 1 for female)

    engine.say(text)  # Convert text to speech
    engine.runAndWait()  # Wait for speech to finish

if __name__ == "__main__":
    text = input("Enter text to convert to speech: ")
    text_to_speech(text)
   

Code Explanation

  • pyttsx3.init() – Initializes the text-to-speech engine.
  • setProperty('rate', 150) – Controls the speech speed (default is 200).
  • setProperty('voice', voices[1].id) – Sets the voice (0 for male, 1 for female).
  • say(text) – Converts text into speech.
  • runAndWait() – Ensures the speech is played before the program exits.
 

Step 3: Running the TTS Converter

Save the script as tts.py and run it using:


python tts.py

Enter any text when prompted, and the program will convert it into speech.

 


Conclusion

With just a few lines of Python, we created a Text-to-Speech converter that can read text aloud. This can be useful for assistive technology, chatbots, audiobooks, and more! 🚀



If you have any questions, feel free to ask in the comments below. Happy coding! 😊