ads

Convert Text to Speech in Python Advance project 2024

 Convert text to speech in Python.

Learn how to convert your text to speech using Python and the Google API

Text-to-speech is the process of converting any text into voice. Text-to-speech projects take words on a digital device and convert them into sound with the click of a button or tap of a finger. Pronunciation Python Project is very useful for people who struggle to read.

 

Project conditions

We will use the basic concepts of Python, Tkinter, gTTS and the Playound library to implement this project.

 

Tkinter is a standard Python GUI library and one of the fastest and easiest ways to build GUI applications using Tkinter.

GTTS (Google Text-to-Speech) is a Python library that converts text to speech very easily.

The game module is used to play audio files. With this module, we can play audio files with one line of code.

To install the required libraries, you can use the pip install command:

1.   pip install tkinter

2.   pip install gTTS

3.   pip install playsound

 

A word on the Python project

The goal of this project is to convert text to sound at the click of a button. This project will be developed using Tkinter, gTT and game library.

In this project, we add the message that we want to convert to audio and press the play button to play the audio of that text message.

 

1.   import module

2.   Create a screen window

3.   Define function

So this is the main step that we will follow in this Python project. Let's get started.

 

1. Import the library

Let's start by importing the libraries: tkinter, gTTS and playound

 

1.   from tkinter import *

2.   import gtts from gtts

3.   Import from game from import game

2. Start window

root = Tk()

1.   geometric root. ("350x300")

2.   root.configure(bg='white ghost')

3.   root.title("DataFlair - PROMISE")

Tk() for the default tkinter is used for the GUI

geometry() is used to determine the width and height of the window

() is used to access window attributes.

bg will be used to set the background color

name () Sets the name of the window

Note ( root , text = "TEXT_TO_SPEECH" , font = "arial 20 bold" , bg = 'white smoke' ) package ()

Label ( text = "DataFlair" , font = 'arial 15 bold' , bg = 'white smoke' , width =

'20') package (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, size = '50')

entry_field.place (x = 20, y = 100)

Technology is evolving fast!

Stay updated with DataFlair on WhatsApp !!

 

The label () widget is used to display one or more strings that cannot be changed by the user.

 

root is the name that refers to our window

the text that we display on the label

text font

Pack the usual tools in the block

Msg is a variable of type string

Input() is used to create an input text field

Used to get the current text into the input widget

place() arranges widgets by placing them in a specific location in the parent widget

3. Convert text to speech in Python

1.   def Text_to_speech():

2.   Message = entry_field.get()

3.   output = gTTS ( text = Message )

4.   output.save('DataFlair.mp3')

5.   play('DataFlair.mp3')

The message variable will hold the input_field value

the text is the sentence or text that is read.

lang get the language to read the text. The default language is English.

use slow to read the text more slowly. default is false.

We don't need to give gTTS the default value lang as we want.

 

words keep the sound changed from the text

speech.save('DataFlair.mp3') will save the converted file into a DataFlair mp3 file

used to play the sound

4. Exit function

1.   def Exit():

2.   root.destroy()

3.   root.destroy () will exit the program by stopping the mainpoint ().

 

5. Reset function

1.   def Reset():

2.   msg.set("")

The reset function sets the Msg variable to the empty string.

 

6. Determine the key

Button ( root , text = "Game" , 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).Ser (x = 175, y = 140)

The button() tool is used to display window buttons

 

 

root.mainloop()

root.mainloop () is the method we execute when we want to run our program.

 

Python text for project output

 


Source Code:

## import libraries

 

from tkinter import *

from gtts import gTTS

from playsound import playsound



################### Initialized window####################

 

root = Tk()

root.geometry('350x300')

root.resizable(0,0)

root.config(bg = 'ghost white')

root.title('DataFlair - TEXT_TO_SPEECH')

 

##heading

Label(root, text = 'TEXT_TO_SPEECH' , font='arial 20 bold' , bg ='white smoke').pack()

Label(root, text ='DataFlair' , font ='arial 15 bold', bg = 'white smoke').pack(side = BOTTOM)




#label

Label(root, text ='Enter Text', font ='arial 15 bold', bg ='white smoke').place(x=20,y=60)

 

##text variable

Msg = StringVar()

 

#Entry

entry_field = Entry(root,textvariable =Msg, width ='50')

entry_field.place(x=20 , y=100)

 

###################define function##############################

 

def Text_to_speech():

    Message = entry_field.get()

    speech = gTTS(text = Message)

    speech.save('DataFlair.mp3')

    playsound('DataFlair.mp3')

 

def Exit():

    root.destroy()

 

def Reset():

    Msg.set("")

 

#Button

Button(root, text = "PLAY" , font = 'arial 15 bold', command = Text_to_speech, width =4).place(x=25, y=140)

Button(root,text = 'EXIT',font = 'arial 15 bold' , command = Exit, bg = 'OrangeRed1').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()

 

 

 

Post a Comment

0 Comments