Skip to content

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.

Notifications You must be signed in to change notification settings

atulkushwaha0112-py/text-to-speech-using-gtts-and-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

text-to-speech-using-gtts-and-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!

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages