Have you ever wanted to make your computer speak out loud using your own Python code? With gTTS (Google Text-to-Speech), you can easily convert any text into an audio file. This is super useful for building voice assistants, making audio notes, or even creating your own AI chatbot's voice.
gTTS stands for Google Text-to-Speech. It is a Python library and CLI tool to extract the spoken text from Google Translate. It's super simple and effective for making basic TTS (Text-to-Speech) programs.
- You type a text.
- The text is sent to Google's TTS API.
- It returns an MP3 audio file.
- You can play the file using any media player.
First, install the library using pip:
pip install gTTS
from gtts import gTTS
import os
text = "Hello! This is your computer speaking."
language = "en"
speech = gTTS(text=text, lang=language, slow=False)
speech.save("output.mp3")
# Play the file (Windows)
os.system("start output.mp3")
# For Linux or Mac
# os.system("mpg321 output.mp3")
🗂️ Commonly gTTS supported languages
af: Afrikaans
ar: Arabic
bn: Bengali
en: English
fr: French
de: German
gu: Gujarati
hi: Hindi
it: Italian
ja: Japanese
kn: Kannada
ml: Malayalam
mr: Marathi
ne: Nepali
pa: Punjabi
ta: Tamil
te: Telugu
ur: Urdu
zh-CN: Chinese (Mandarin/China)
slow=False: ✅ Normal speed (default, ideal for general use)
slow=True: 🐢 Slower speed, useful for language learners or clarity
This version lets the user type custom text to convert:
from gtts import gTTS
import os
text = input("Enter the text you want to convert to speech: ")
language = "en"
speech = gTTS(text=text, lang=language, slow=False)
filename = "custom_voice.mp3"
speech.save(filename)
# Play the file (Windows)
os.system(f"start {filename}")
- 🎧 Use different languages by changing
lang
like'hi'
for Hindi,'fr'
for French, etc. - 🗂️ Combine gTTS with
pyttsx3
to create offline and online versions. - 🎛️ Adjust speed using
slow=True
if you want a slow voice (good for teaching tools). - 🧪 Combine with
speech_recognition
to create a full voice assistant!
os.system("start output.mp3") # plays using default player
os.system("mpg321 output.mp3")
os.system("afplay output.mp3")
gTTS is one of the easiest ways to add voice to your Python projects. From making talking robots to automating announcements or creating fun tools, the possibilities are endless.
Want to go even deeper? Try combining gTTS with:
- Flask – to make a voice bot website
- Telegram Bot – to make a speaking chatbot
- Audio Editor – use
pydub
for trimming/combining audio
Hope this guide helped you! Share your voice bot in the comments!