-2

How could I make Python to speak some text ?

I have heard that there is a Python TTS library. How to include that in the program ? and which function should I call with a given text to speak though CPU speakers.

engr_john
  • 27
  • 2
  • 1
    SO is not a tutorial site, we are happy to help with specific problems in your code but can't and won't provide complete solutions. Have a read through this https://stackoverflow.com/help/how-to-ask – Erik Sep 04 '21 at 09:47

1 Answers1

1

You will need the module : gTTS (Google Text-to-Speech)

First of all, install the module gTTS

pip install gTTS

Then create a python file :

from gtts import gTTS

#Write your text
text_to_speech = "So that's how you convert text to speech using Python"

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

#Passing the text, language and the speed to the module
myobj = gTTS(text=text_to_speech, lang=language, slow=False)

#Saving the audio in mp3
myobj.save("text_to_speech.mp3")



Adil
  • 38
  • 5
  • I have installed "pip install gTTS" and make a python file and run the above code. There is no error but there is no sound. The speakers are on when I pay some youtube video. How to get the text really speak – engr_john Sep 08 '21 at 14:38
  • Have you checked the mp3 file that was created in the folder ? – Adil Sep 08 '21 at 21:11
  • Hi, yes mp3 is created but my program is real time and I would like to speak the text in python in real time. Any idea how to do this ? – engr_john Sep 09 '21 at 07:51
  • Hi check this post https://stackoverflow.com/questions/51164040/gtts-direct-output – Adil Sep 21 '21 at 19:48