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.
- Open your command terminal in window and install gtts module by using below command.
- Open your IDLE or Visual studio code and select new file import the module.
- Now we have give the file name. Remember the file name also contain extension.
- Create a text-file in notepad and place that file along with python file otherwise you have provide file address
- Now we have save audio file which will created.
- If file is not create.
pip install gtts
#module
from gtts import gTTS
#filename
filename=input('Choose the filename with extension 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 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.
Label:
Pycodes