Text to Speech in Python (Learn Python with Akshit Madan)

Hey readers ! In this blog we are going to see how you can implement a text to speech model in python using gtts package that’s is Google Text to Speech package . The model takes a .txt file as input and reads the file line by line .

So, let’s code !!

First we imported gtts and os libraries, then we opened our text file, please mention the full path of the file if your file exists in folder other than the current one.

As you can see we mentioned .replace() function and replaced all the next line tags with the blank spaces so that the gtts package should work efficiently. Well I recommend you to even try without using replace function and notice what happens.

After reading the file , we mentioned the language of the text and stored the audio result in a file output.mp4, here also mention the complete output path.

And yes you are ready with your own audio book !!



Flutter Developer | ML | AR

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store