Basic Text to Speech application - Text to Speech Convertor using gTTS library in Python

Опубликовано: 02 Июнь 2026
на канале: OS_LEARNING
20
0

This is a basic example of how to use gTTS to create a basic text-to-speech application in Python:

1. First, you need to install the gTTS library if you haven't already. You can do this using pip:

pip install gTTS

2. Once you have gTTS installed, you can create a Python script like this:

from gtts import gTTS
import os

Define the text you want to convert to speech

text = "Thank you for watching! if you enjoyed this video, please hit the like button and subscribe to my channel for updates. Thank you"

Create a gTTS object

tts = gTTS(text)

Save the generated speech to an audio file

tts.save("output.mp3")

Play the audio file (you can use different methods to play audio, depending on your OS)

#os.system("mpg321 output.mp3") # For Linux
os.system("afplay output.mp3") # For macOS
os.system("start output.mp3") # For Windows

***Let recapture what have been done in this example:***

We import the gTTS class from the gtts library.
We define the text you want to convert to speech.
We create a gTTS object by passing the text as an argument.
We save the generated speech to an audio file named "output.mp3".
Finally, we use the os.system function to play the audio file. Make sure you have an appropriate audio player installed on your system (e.g., mpg321 for Linux, afplay for macOS, or start for Windows).

Link to download Python: https://www.python.org/downloads/