Python Text to Speech with Full Source Code For Beginners

When executed the text from abc.txt will be turned into an mp3, saved and then played on your device.

Prerequisites:

  • abc.txt with your text
  • the gTTS==2.1.1 module (pip install gTTS to download)
  • the os module (pip install os)

Run the Script:

  • Write your desired text into the abc.txt file then execute the txtToSpeech.py file.
  • This can be done by typing ‘python txtToSpeech.py’ into your Terminal.

Source Code:

txtToSpeech.py

from gtts import gTTS 
import os
file = open("abc.txt", "r").read()

speech = gTTS(text=file, lang='en', slow=False)
speech.save("voice.mp3")
os.system("voice.mp3")

#print(file)Code language: PHP (php)

abc.txt

Thanks to Gail Cleaver, Beth Barrack, Bingo Nightly, Emily Webber and Sharon Counts. Finally, special thanks to Casey Cromwell. Radio Lab is produced by WNYC New York public radio, and distributed by NPR, National Public Radio.
Code language: PHP (php)

Leave a Comment