Have you ever wanted your computer to speak like J.A.R.V.I.S. from Iron Man? With Python, this is totally possible—and easier than you think!
In this beginner-friendly tutorial, we’ll show you how to create your very own Text-to-Speech (TTS) program using Python. Whether you’re building assistive technology or just exploring voice capabilities, this simple project will get you started.
🔧 What You’ll Need
Before diving in, make sure you have:
- Python installed (preferably Python 3.6+)
- A package called
pyttsx3
(we’ll install it in a second)
🚀 Installing pyttsx3
Open your terminal or command prompt and run:
pip install pyttsx3
pyttsx3
is an offline, cross-platform Text-to-Speech conversion library in Python. It supports multiple TTS engines and lets you change voices, volume, and even speech rate!
💡 The Code
Here’s the complete Python script:
import pyttsx3
# Initialize the Text-to-Speech engine
engine = pyttsx3.init()
# Get available voices
voices = engine.getProperty('voices')
# Set a voice (0 for male, 1 for female - may vary by OS)
engine.setProperty('voice', voices[1].id)
# Set speech rate (words per minute)
engine.setProperty('rate', 150)
# Get user input
text = input("Enter something to speak: ")
# Speak the text
engine.say(text)
engine.runAndWait()
🔍 What’s Happening Behind the Scenes?
pyttsx3.init()
initializes the TTS engine.
getProperty('voices')
retrieves the available system voices.
setProperty('voice', voices[1].id)
sets the voice. Index 1
is typically a female voice (you can experiment).
engine.say(text)
queues the text to speak.
engine.runAndWait()
processes and speaks the text.
🧪 Sample Output
Input:
Enter something to speak: Hello, I am your Python assistant.
Output:
Your system will speak:
“Hello, I am your Python assistant.”
Cool, right? 🧠🔊
💼 Where Can You Use This?
- Screen readers for the visually impaired
- Fun AI assistant projects
- Voice alerts in automation scripts
- Chatbots with voice feedback
📌 Tips
- If the voice doesn’t sound right, try changing the
voices[1]
tovoices[0]
.
- You can adjust the speed by changing the
rate
to a higher or lower number.
🔚 Final Thoughts
This simple yet powerful Text-to-Speech project is a great stepping stone into the world of AI and voice technology. With just a few lines of Python code, you’ve brought your program to life with a voice.
Feel free to explore more by integrating this with GUIs like Tkinter or combining it with speech recognition for two-way voice communication!
🔔 Don’t forget to subscribe to our YouTube channel Everything About Coding for the video tutorial version of this project!