In this tutorial, you will learn how you can convert text to speech in Python. It converts human language text into human-like speech audio. Speech synthesis (or Text to Speech) is the computer-generated simulation of human speech. If this article sounds informative then make sure to follow and share it with your geek community.Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission. #import the libraries from gtts import gTTS from playsound import playsound from tkinter import * #initialized window root = Tk() root.geometry('600圆00') root.title('BUG NINZA') nfig(bg='white') #Labels Label(root, text = 'TEXT TO SPEECH', font='arial 20 bold', bg ='white smoke').pack() Label(root, text ='Enter Text', font ='arial 15 bold', bg ='white smoke').place(x=20,y=60) # text variable Msg = StringVar() #Entry input_field = Entry(root, textvariable=Msg, width = '60') input_ace(x=20, y=100) #tts function def tts(): Message = input_field.get() speech = gTTS(text = Message) speech.save('Ninza.mp3') playsound('Ninza.mp3') def Exit(): stroy() def Reset(): Msg.set("") #Buttons Button(root, text = "PLAY", font = 'arial 15 bold', command = tts, width =4).place(x=25, y=140) Button(root,text = 'EXIT',font = 'arial 15 bold', command = Exit, bg = 'Red').place(x=100,y=140) Button(root, text = 'RESET', font='arial 15 bold', command = Reset).place(x=175, y =140) #infinite loop to run program root.mainloop() If you lost somewhere in the code then here is the full code of this project. This is the audio file of the text you played recently. You will see one new mp3 file(Ninza,mp3) there. Now check the directory in which you made this Python file. Text to speech conversion using gtts pythonĮnter your name in the input area and click on the play button. You will see something like this on-screen. Well, that’s it, now runs the Python program in the virtual environment, using the command: python tts.py Play button, exit button, and a reset button. Next, the rest two functions are to exit the window and reset the input space.īutton(root, text = "PLAY", font = 'arial 15 bold', command = tts, width =4).place(x=25, y=140) Button(root,text = 'EXIT',font = 'arial 15 bold', command = Exit, bg = 'Red').place(x=100,y=140) Button(root, text = 'RESET', font='arial 15 bold', command = Reset).place(x=175, y =140) We are using google text to speech library for the conversion of text into speech. You can play as well as save the audio of the text in your local storage. In the tts function, we get the message from the input field variable. That’s why we make a variable for it.ĭef tts(): Message = input_field.get() speech = gTTS(text = Message) speech.save('Ninza.mp3') playsound('Ninza.mp3') def Exit(): stroy() def Reset(): Msg.set("") We are using the input field from tkinter Python library and it requires text variable as a parameter. #text variable Msg = StringVar() #Entry input_field = Entry(root, textvariable=Msg, width = '60') input_ace(x=20, y=100) We set the format of text and take care of its position on the screen. Label(root, text = 'TEXT TO SPEECH', font='arial 20 bold', bg ='white smoke').pack() Label(root, text ='Enter Text', font ='arial 15 bold', bg ='white smoke').place(x=20,y=60) The background color of the window will be white with the title “BUG NINZA”. In the above code, I just assign a window screen with a dimension of 600*600. we just have to assign value as per our choice. We need some GUI, right? And Tkinter will take care of it. Code Partįrom gtts import gTTS #google text to speech from playsound import playsound from tkinter import * Make a Python file in the virtual environment and name it as per your choice. pip install tk #make sure tkinter installed in your system pip install playsound pip install gtts If you are using Linux then you can type the “ source bin/activate” command in your terminal window to activate the virtual environment.Īfter you activate the virtual environment then install the following Python library package. The code for it is different for different OS. Navigate to the directory “textToSpeech” and activate the virtual environment. Then open the terminal window or command prompt window in your preferred directory. If you don’t have a virtual environment installed in your system then install it first. It’s a good habit to build a Python-based project in a virtual environment. I recommend you to make this project in a virtual environment. Text to Speech Conversion Code with Explanation
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |