How to convert Text to Speech using Python code

Hello friends, Recently I was preparing a youtube video for which I wanted to use a good professional voiceover audio.

But when I tried to check out some of the online tools, most of them were either having some limitations on the number of conversion allowed or they required me to register for premium subscription to use the service.

Eventually I decided to do that using a quick code in Python. Here's the Python code to convert text to speech.

In this code, we are using gTTS library. If you do not have gTTS module installed in your Python environment, you can install it using the following command:

                
pip install gTTS
                
                        

Once it is installed, you can write the below python scripts in your IDE and run the code

                
from gtts import gTTS
txt="I am a text to speech converter"

myobj = gTTS(txt, lang='en', slow=False)
myobj.save("3.mp3")

                

In this code, we are using gTTS module which is Google's Text To Speech module. If you are interested in exploring this library further you can visit gTTS documentation page.

In this code, we are creating a variable 'txt' to store the text. In the next step, we are passing this text to the gTTS function. This function also takes a few additional parameters such as language and speed. In the next step, the file is saved in '.mp3' format in your local directory.

Please note that this module uses the cloud based gTTS module, hence you will require live internet connection while using this module.

Content is copyrighted © www.123mylist.com