Text-to-Speech (TTS) in Python using pyttsx3



๐ŸŽ™️ Text-to-Speech (TTS) in Python using pyttsx3 for Windows

pyttsx3 is a Python library that converts text into spoken words using Windows' built-in voice engine. It works offline and lets you control voice, rate, and volume.

๐Ÿ”ง Step 1: Install pyttsx3

pip install pyttsx3

๐Ÿง  Step 2: Basic Example


import pyttsx3

engine = pyttsx3.init()
engine.say("Hello, welcome to pyttsx3 text to speech!")
engine.runAndWait()

๐Ÿ’ก Step 3: List All Supported Voices


import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')

for idx, voice in enumerate(voices):
    print(f"{idx}. Name: {voice.name}, ID: {voice.id}, Lang: {voice.languages}")

You’ll typically see voices like:


0. David - English (US) - Male
1. Zira - English (US) - Female

๐ŸŽ™️ Change Voice: Male / Female


engine.setProperty('voice', voices[0].id)  # Male
engine.setProperty('voice', voices[1].id)  # Female
Note - It support only two Types of Voices But you can change Pitch

⚙️ Set Rate, Volume, and Speak


engine.setProperty('rate', 150)    # Default ~200
engine.setProperty('volume', 1.0)  # Range: 0.0 to 1.0

engine.say("This is a custom voice with speed and volume.")
engine.runAndWait()

๐ŸŒ Supported Languages

pyttsx3 uses system-installed voices. You can add new voices in Windows:

๐Ÿ—ฃ️ How to Install More Text-to-Speech Voices in Windows (for pyttsx3)

If you want more voices like Hindi, Spanish, French, or regional English (India/UK/US) in your pyttsx3 project, follow these easy steps:

✅ Step-by-Step Guide

  1. ๐Ÿ› ️ Open Settings → Press Win + I
  2. ⏳ Go to Time & Language > Speech
  3. ๐Ÿงฉ Scroll down to Manage Voices (in Windows 11)
  4. ➕ Click Add voices
  5. ๐ŸŒ Choose from languages like:
    • English (India)
    • Hindi
    • Spanish, French, etc.
  6. ⬇️ Wait for the download to finish
  7. ๐Ÿ” Restart your PC to activate new voices

๐Ÿ’ป Python Code: List All Installed Voices


import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')

for idx, voice in enumerate(voices):
    print(f"{idx}. Name: {voice.name}, ID: {voice.id}, Lang: {voice.languages}")

๐ŸŽฏ Example Output:


0. Microsoft David Desktop - English (US)
1. Microsoft Zira Desktop - English (US)
2. Microsoft Ravi - English (India)
3. Microsoft Heera - Hindi
4. Microsoft Helena - Spanish (Spain)

๐Ÿง  How to Use a Specific Voice (by Index or Language)

๐Ÿ‘‰ Using voice by index:


engine.setProperty('voice', voices[2].id)  # Ravi - English (India)

๐Ÿ‘‰ Auto-select by language (e.g., Hindi):


for voice in voices:
    if 'hi-IN' in voice.languages or 'Hindi' in voice.name:
        engine.setProperty('voice', voice.id)
        break

๐Ÿ’ก Tips

  • Use voice.languages to detect language codes like 'en-IN', 'hi-IN', 'es-ES'
  • Voices must be installed via Windows Settings first
  • Always restart after installing new voices

๐ŸŽ™️ Save Voice Output as MP3 (Optional)


engine.save_to_file("เคฏเคน เค†เคตाเคœ़ เคซाเค‡เคฒ เคฎें เคธेเคต เคนोเค—ी", "output.mp3")
engine.runAndWait()

๐Ÿ“Œ Conclusion

Using pyttsx3 with Windows' built-in and custom-installed voices lets you create advanced text-to-speech apps in multiple languages — fully offline!

๐Ÿ“‚ Full Project Example


import pyttsx3

engine = pyttsx3.init()

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)  # Female

engine.setProperty('rate', 140)
engine.setProperty('volume', 1.0)

text = "Hello! This is a text-to-speech demo using pyttsx3 on Windows."
engine.say(text)
engine.runAndWait()

๐Ÿง  Tricks & Hacks

  • Use engine.save_to_file("Text", "output.mp3") to save TTS as audio file.
  • Install more Windows voices for different languages.
  • Create a voice menu in your app for switching between voices live.

❓ FAQ

Q. Does pyttsx3 work offline?
✅ Yes, it is fully offline as it uses built-in TTS engines.

Q. Can I change the language?
✅ Yes, if that language's voice is installed on your Windows system.

Q. How to slow down the voice?
๐ŸŽš️ Use engine.setProperty('rate', 120)

๐Ÿ“ฆ Extra Tip: Save to MP3 File


engine.save_to_file("Save this text to file", "speech.mp3")
engine.runAndWait()

๐ŸŽฏ Conclusion

pyttsx3 is a powerful and flexible offline TTS library for Python developers on Windows. Customize voices, export audio, and even integrate it in GUIs or games!

Previous Post Next Post

Contact Form