Text-to-Speech Converter using gTTS in Python



๐ŸŽค Text-to-Speech Converter using gTTS in Python

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.


๐Ÿ”ง What is gTTS?

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.


✅ How It Works

  • 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.

๐Ÿ’ก Installation

First, install the library using pip:

pip install gTTS

๐Ÿ‘ถ Basic Example Code


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)

๐Ÿข What does slow=False mean?

The slow parameter controls speech speed:


slow=False: ✅ Normal speed (default, ideal for general use)
slow=True: ๐Ÿข Slower speed, useful for language learners or clarity

๐Ÿง  Advanced Version with Input

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}")

⚙️ Tips, Tricks, and Hacks

  • ๐ŸŽง 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!

๐Ÿ“ฑ Use on Different Devices

๐Ÿ’ป Windows


os.system("start output.mp3")  # plays using default player

๐Ÿง Linux


os.system("mpg321 output.mp3")

๐Ÿ MacOS


os.system("afplay output.mp3")

๐Ÿš€ Final Thoughts

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!

Previous Post Next Post

Contact Form