Text to Speech Converter – Python Code

Do not want to read a long text file? Instead of that you want to listen the whole concept/story at your own time, though there are a lots of such Text to speech converter is already out there but I think it is best for python programmer to design their own Text to speech converter that is only 10 lines long but will help you a lot and will also help you to revise your notes and vital information when you have limited time.

Here is the python code of Text to speech converter

# Import the required module for text to speech conversion

from gtts import gTTS
from playsound import playsound
 
# This module is imported so that we can  play the converted audio

import os
 
# The text that you want to convert to audio

mytext = 'My Baby samraddhi is playing with my mobile phone. Welcome to cbsetoday.com and this is really amazing my dear!'
 
# Language in which you want to convert
language = 'en'
 
# Passing the text and language to the engine, 
# here we have marked slow=False. Which tells 
# the module that the converted audio should 
# have a high speed

myobj = gTTS(text=mytext, lang=language, slow=False)
 
# Saving the converted audio in a mp3 file named
# welcome 
myobj.save("welcome.mp3")
 
# Playing the converted file
#os.system("mpg321 welcome.mp3")
playsound("welcome.mp3")

How to run the above code

  1. Python Module of Google text to speech converter – gTTS
  2. playground Python Module for playing your sound File.

Suggestion for Text to Speech Converter

We have converter only a single line of Text using the above code but you can easily enhance this code using Text file handling.

 

Leave a Reply