Convert any text-file into audio-file using python(Text-To-Speech)

    Friends we can convert any text-file into audio format by using python but for this your computer should connected to the internet other wise it show some error and your audio file will not work . So let start the coding.

  1. Open your command terminal in window and install gtts module by using below command.
  2. pip install gtts
  3. Open your IDLE or Visual studio code and select new file import the module.
  4. #module
    from gtts import gTTS
  5. Now we have give the file name. Remember the file name also contain extension.
  6. #filename
    filename=input('Choose the filename with extension mp3:- ')
  7. Create a text-file in notepad and place that file along with python file otherwise you have provide file address
  8. file=open("textfile.txt")
    myobj = gTTS(text = file.read(), slow=False)
  9. Now we have save audio file which will created.
  10. try:
        myobj.save(filename)
        print('File has been created')
  11. If file is not create.
  12. except:
        print('An error occurred')

Complete Code

from gtts import gTTS

filename=input('Choose the filename with exention mp3:- ')

file=open("textfile.txt")
myobj = gTTS(text = file.read(), slow=False)

try:
    myobj.save(filename)
    print('File has been created')
    
except:
    print('An error occured')

or

    Code was completed and the audio file was saved on the same address where the your python file have been saved. What you want to get in audio put that into text file but remember language should be English or you can used Hinglish also. If you got any error put it on comment section.

Previous Post Next Post