Speaking a simple text in python


I am running the following script, there is no error but there is no sound. I am not sure how I can make the text really speak through the speakers. Any idea ?

from gtts import gTTS

#Write your text
text_to_speech = "Text to speech using Python"

#Choose your language (en : english, fr: french, ...)
language = "en"

i = 1
while i <= 5: 
  #Passing the text, language and the speed to the module
  myobj = gTTS(text=text_to_speech, lang=language, slow=False)

The gTTS library can only write to MP3 files. See https://github.com/pndurette/gTTS/issues/26

Here’s another list of libraries that can then play the MP3 file.

You have to use myobj.save to save to an mp3 file, and then play the
mp3 file using whatever program you would normally use to play mp3
files. On Linux, you may be able to use mpg123.

Also, you should not use a while loop.

If you are running Linux, and you have mpg123 installed, you can try

import os
from gtts import gTTS
text_to_speech = "Text to speech using Python"
myobj = gTTS(text=text_to_speech, lang='en', slow=False)

for i in range(5):
    os.system('mpg123 my_text.mp3')

See the documentation:


I am using Windows. If I add the following code then it save the mp3 file which I can play, that’s fine but I wat to make the text speak in real time and not saving in mp3 format.

#Saving the audio in mp3