Convert Text to Speech in Python
Build your Python skills with hands-on projects and become job-ready!
Project Prerequisites
This project uses basic Python concepts along with the following libraries:
Tkinter – A built-in Python library for creating graphical user interfaces.
gTTS (Google Text-to-Speech) – Converts text into audio using Google’s API.
playsound – Plays audio files with a simple command.
Use the following pip commands to install the necessary libraries:
bash
pip install tkinter pip install gTTS pip install playsound
Project Overview
The goal is to input any message, click a button, and hear the message spoken aloud. We'll use Tkinter for the GUI, gTTS to convert the text to audio, and playsound to play the audio.
Steps to Build the Project
1. Import Required Libraries
python
from tkinter import * from gtts import gTTS from playsound import playsound
2. Initialize the Window
python
root = Tk() root.geometry("350x300") root.configure(bg='ghost white') root.title("Debugshala - TEXT TO SPEECH")
Add labels and input fields:
python
Label(root, text="TEXT_TO_SPEECH", font="arial 20 bold", bg='white smoke').pack() Label(text="Debugshala", font='arial 15 bold', bg='white smoke', width='20').pack(side='bottom') Msg = StringVar() Label(root, text="Enter Text", font='arial 15 bold', bg='white smoke').place(x=20, y=60) entry_field = Entry(root, textvariable=Msg, width='50') entry_field.place(x=20, y=100)
3. Convert Text to Speech Function
python
def Text_to_speech(): Message = entry_field.get() speech = gTTS(text=Message) speech.save('Debugshala.mp3') playsound('Debugshala.mp3')
4. Exit Function
python
def Exit(): root.destroy()
5. Reset Function
python
def Reset(): Msg.set("")
6. Create Buttons
python
Button(root, text="PLAY", font='arial 15 bold', command=Text_to_speech, width='4').place(x=25, y=140) Button(root, font='arial 15 bold', text='EXIT', width='4', command=Exit, bg='OrangeRed1').place(x=100, y=140) Button(root, font='arial 15 bold', text='RESET', width='6', command=Reset).place(x=175, y=140)
7. Run the Mainloop
python
root.mainloop()
Project Summary
You’ve successfully built a Python text-to-speech converter! This project used Tkinter to design the GUI, gTTS to turn text into voice, and playsound to play the generated audio. It’s a simple yet powerful project that showcases the practical use of Python for real-world applications.
Write A Comment
No Comments