๐️ 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
- ๐ ️ Open Settings → Press
Win + I
- ⏳ Go to Time & Language > Speech
- ๐งฉ Scroll down to Manage Voices (in Windows 11)
- ➕ Click Add voices
- ๐ Choose from languages like:
- ✅ English (India)
- ✅ Hindi
- ✅ Spanish, French, etc.
- ⬇️ Wait for the download to finish
- ๐ 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!