Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DuncanWilliamGibbons/121f169584ee8a74e9c8eeac38ca2196 to your computer and use it in GitHub Desktop.
Save DuncanWilliamGibbons/121f169584ee8a74e9c8eeac38ca2196 to your computer and use it in GitHub Desktop.
TEXT TO SPEECH IN PYTHON | Convert Text to Speech in Python
# Import the Gtts module for text
# to speech conversion
from gtts import gTTS
# import Os module to start the audio file
import os
fh = open("test.txt", "r")
myText = fh.read().replace("\n", " ")
# Language we want to use
language = 'en'
output = gTTS(text=myText, lang=language, slow=False)
output.save("output.mp3")
fh.close()
# Play the converted file
os.system("start output.mp3")
# Import the Gtts module for text
# to speech conversion
from gtts import gTTS
# import Os module to start the audio file
import os
mytext = 'Convert this Text to Speech in Python'
# Language we want to use
language = 'en'
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("output.mp3")
# Play the converted file
os.system("start output.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment