This repository contains a simple Python script to generate speech from text using the gTTS
(Google Text-to-Speech) library.
Text-to-speech (TTS) technology converts written text into spoken words. This project demonstrates how to use the gTTS
library to create an MP3 file from a given text string.
Before you begin, ensure you have Python installed on your system. You can install the gTTS
library using pip
.
pip install gTTS
Here is the Python script to generate speech from text:
from gtts import gTTS
language = "en"
text = "Hello, guys how are you doing? I hope you all are doing well."
speech = gTTS(text=text, lang=language, slow=False, tld="com.au")
speech.save("textToSpeech.mp3")
from gtts import gTTS
: Imports thegTTS
class from thegTTS
module.language = "en"
: Sets the language to English.text = "Hello, guys how are you doing? I hope you all are doing well."
: The text you want to convert to speech.speech = gTTS(text=text, lang=language, slow=False, tld="com.au")
: Creates agTTS
object with the specified text, language, speed (slow=False for normal speed), and tld (Top Level Domain) to use the Australian English accent.speech.save("textToSpeech.mp3")
: Saves the generated speech to an MP3 file namedtextToSpeech.mp3
.
Save the script to a file, for example text_to_speech.py
, and run it using the Python interpreter:
python text_to_speech.py
This will generate an MP3 file named textToSpeech.mp3
in the same directory.