Are you bored to read long articles and want someone else to read all that boring stuff you want to know from that text file. You can do this using today’s sample code that can convert any text into speech and will also play that sound file for you.
For this Text to speech, You need Google Text to Speech engine that you have to install using pip command on your system. Here is the python code for text to speech engine.
# Import the required module for text # to speech conversion from gtts import gTTS from playsound import playsound # The text that you want to convert to audio mytext = 'Welcome to cbsetoday.com and the website is going to provide some really good python programs for cbse students!' # 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")
Python Modules required to run Text to speech program
The above program needs two basic python modules
- gTTS python Module is basically Google module for Text to speech conversion.
- playsound is a general purpose music player module for python and it is used to basically sound file.
How to Run Text to speech Program
- First of all install above mentioned python modules in any order. Without these modules this program will not work at all.
- This program was tested on windows 7-10 using python 3.6
Final note: Though this program was implemented on a simple string but you can use any test file to listen its text. If you are able to find any bug, please send your voice via comments.