[python] How to make Python speak

I prefer to use the Google Text To Speech library because it has a more natural voice.

from gtts import gTTS
def speak(text):
  tts = gTTS(text=text, lang="en")
  filename = "voice.mp3"
  tts.save(filename)

There is one limitation. gTTS can only convert text to speech and save. So you will have to find another module or function to play that file. (Ex: playsound)

Playsound is a very simple module that has one function, which is to play sound.

import playsound
def play(filename):
  playsound.playsound(filename)

You can call playsound.playsound() directly after saving the mp3 file.