Text to speak python Project

NA PROJECT REPORT

(Read To Speak)

Project File Structure

1.      Importing various modules

2.     Initializing main window

3.     Read to speak function

4.     Voice change

5.     Rest Code

 

Read to speak is a process to convert any text into voice. Read to speak project takes words on digital devices and convert them into audio. Here I have used Google-text-to-speech library popularly known as gTTS library to convert text file to .mp3 file. The gTTS library supports several languages including English, French, German and many more. And before getting started in this project you have to install couple of python libraries like gTTS and playsound. In order to download the libraries, open your terminal and write (pip install gtts) and (pip install playsound). In this project I have written the description of each line of code, this will make you clear with the coding section! I have attached the file (icon of my window) with the exact code in this project. Now you are all set to run the program. Hope you like this project! Thank you.

OR

Read to speak is a Python program and can be used to read text. Currently it has English language reading support via CLI arguments. For other language support you need to configure the code accordingly.

Requirements

System requirements:
  • You must have Python 3 installed in your system.
  • All commands should be run in Python 3 environment.
Prerequisite knowledge:
  • Basic understanding of Python
  • Basic understanding of Terminal or Command Prompt

 

Getting Started

  1. Install the required python packages
pip install pyttsx3
pip install pypiwin32 # Windows only
  1. Run the project
python main.py

 

Command Line Options

  1. -t or --text for cli input of text.
 
python main.py -t "This is input from command line"
  1. -l or --language for cli input of language. Currently it support are English. Available inputs are English.
 
 
 
 
 
python main.py -t Hello -l english
  1. -a or --accent for cli input of language accent. Only available for english language.
 
python main.py -t Hello -l english -a uk
  1. -g or --gender for cli input of voice gender. Available inputs are male and female. By default it is set to male.
 
python main.py -t Hello -l english -a uk -g female
  1. -i or --index to select rank of the reader form the output list. It starts from 0 (Zero). If you don't select any index it will automatically take the first one.
python main.py -t Hello -l english -a us -g female -i 1
 

NOTE : Sometimes there might be chances that the readers are not available for specific gender or language or accent. In that case, it tries to find alternative or else it will read by the default reader which has English language, US accent and voice gender is male

 code 


import tkinter as tk                # importing tkinter package

from tkinter import ttk,filedialog
from tkinter import *
from tkinter import SUNKEN
import random
import  pyttsx3
# import PIL.Image
# from PIL import Image
import  os

# ************************End of Import*************************************
e= pyttsx3.init()
win= tk.Tk()
win.iconbitmap('icon.ico')
win.title('Text to speak')
win.geometry('1000x600')
# win.minsize(width=200,height=500)
# win.maxsize(width=200,height=500)

# ************************Function Create*************************************
#=========================Tile function=======================================
def TitleColoreChange():
    color=["green","blue","Red","pink","yellow"]
    c= random.choice(color)
    lblToptitle.configure(background=c)
    lblToptitle.after(100,TitleColoreChange)
#=========================Tile function End===================================
#=========================Text to Voice function=======================================
def TextToConvertVoive():
    def ChangeVoice():
        if (VoiceChange == 'Male'):

            e.setProperty('voice', voices[0].id)  # change index to change voices
            # print(volum)
            e.setProperty('volume', volum /100)
            e.say(text)

            e.runAndWait()
        else:
            e.setProperty('voice', voices[1].id)  # change index to change voices
            e.setProperty('volume', volum/100)
            e.say(text)
            e.runAndWait()

    text=txtRead.get(0.0,END)
    VoiceChange=cmbxVoiceChangeSelect.get()
    Speed=cmbxSpeedSelect.get()
    volum=ScaleVolumSelect.get()
    voices = e.getProperty('voices')
    if(text):
        if(Speed=='Fast'):
            e.setProperty('rate',300)
            ChangeVoice()
        if (Speed == 'Normal'):
            e.setProperty('rate', 150)
            ChangeVoice()
        if (Speed == 'slow'):
            e.setProperty('rate', 50)
            ChangeVoice()

#=========================Text to Voice End===================================
#=========================Download Voice function=======================================
def DownloadVoive():
    def ChangeVoice():
        if (VoiceChange == 'Male'):

            e.setProperty('voice', voices[0].id)  # change index to change voices
            e.setProperty('volum', volum / 50)
            # e.say(text)
            Path= filedialog.askdirectory()
            os.chdir(Path)
            e.save_to_file(text,'music.mp3')
            e.runAndWait()
        else:
            e.setProperty('voice', voices[1].id)  # change index to change voices
            e.setProperty('volum', volum / 50)
            Path = filedialog.askdirectory()
            os.chdir(Path)
            e.save_to_file(text, 'music.mp3')
            e.runAndWait()

    text=txtRead.get(0.0,END)
    VoiceChange=cmbxVoiceChangeSelect.get()
    Speed=cmbxSpeedSelect.get()
    volum=ScaleVolumSelect.get()
    voices = e.getProperty('voices')
    if(text):
        if(Speed=='Fast'):
            e.setProperty('rate',300)
            ChangeVoice()
        if (Speed == 'Normal'):
            e.setProperty('rate', 150)
            ChangeVoice()
        if (Speed == 'slow'):
            e.setProperty('rate', 50)
            ChangeVoice()

#=========================Download End===================================
# ************************Function Create End ********************************
# ----------------------lblToptitless----------------------
lblToptitle =ttk.Label(win,text='Text to speak project', font='arail 50 bold', anchor=CENTER )
lblToptitle.place(x=0, y=0,relwidth=1)
# ----------------------lblToptitle End ----------------------
# ----------------------lblRead----------------------
lblRead= tk.Label(text='Enter a Text')
lblRead.place(x=200, y=85)
# ----------------------lblRead End ----------------------

# ----------------------readtext----------------------
readtext=tk.StringVar()
# Add a Scrollbar(horizontal)
v=Scrollbar(win, orient=HORIZONTAL)
v.pack(side=RIGHT, fill=Y)

txtRead=tk.Text(win,width=50, relief=SUNKEN ,height=10,font=('time new romain ',15,'bold'),bg='grey99', yscrollcommand=v.set,wrap=WORD)
v.config(command=txtRead.yview)
txtRead.place(x=10, y=110)

# ----------------------readtext End ----------------------
# ----------------------lblVoiceChange----------------------

VoiceChangepic = PhotoImage(file="2.png" )
lblVoiceChange= Label(image=VoiceChangepic )

lblVoiceChange.place(x=10, y=380)
# ----------------------lblVoiceChange End ----------------------
# ----------------------lblSpeed----------------------
SpeedContolpic = PhotoImage(file="speedcontrol.png" )
lblSpeed= Label(image=SpeedContolpic)
lblSpeed.place(x=250, y=380)
# ----------------------lblSpeed End ----------------------
# ----------------------lblVolum----------------------
VolumContolpic = PhotoImage(file="volumcontrol.png" )
lblVolum= Label(image=VolumContolpic)
lblVolum.place(x=480, y=380)
# ----------------------lblVolum End ----------------------

# ----------------------comboboxGender----------------------
cmbxVoiceChangeSelect=ttk.Combobox(win,values=['Male','Female'],font='arical 13 bold',state='readonly')
cmbxVoiceChangeSelect.place(x=10, y=460)
cmbxVoiceChangeSelect.current(0)
# ----------------------comboboxSpeed----------------------
cmbxSpeedSelect=ttk.Combobox(win,values=['Normal','Fast','slow'],font='arical 13 bold',state='readonly')
cmbxSpeedSelect.place(x=250, y=460)
cmbxSpeedSelect.current(0)
# ----------------------ScaleVolum----------------------
ScaleVolumSelect=tk.Scale(win,from_=0 ,to=100, orient=HORIZONTAL,length=160 )
ScaleVolumSelect.place(x=490, y=443)
ScaleVolumSelect.set(50)
# ----------------------ConvertovoiceButton----------------------
Convertovoicepic = PhotoImage(file="convert.png" )
# lblVolum= Label(image=PlayButtonpic)
btn_Convertovoice=Button(win , image=Convertovoicepic,activebackground='white',command=TextToConvertVoive)
btn_Convertovoice.place(x=100, y=523)
# ----------------------DownloadButton----------------------
Downloadpic = PhotoImage(file="Download.png" )
# lblVolum= Label(image=PlayButtonpic)
btn_Download=Button(win , image=Downloadpic,activebackground='white', command=DownloadVoive)
# btn_Download=Button(win , font='arial 17 bold',bg='lime' ,activebackground='yellow', relief=SUNKEN,text='Download' ,width=18)
btn_Download.place(x=400, y=523)

# win.configure(bg='gray')
TitleColoreChange()


# load = Image.open('icon.ico')
# render = PhotoImage(load)
# img = Label(image=photo)
# # img.image = render
# img.place(x=0,y=0)


# ----------------------Windows form end----------------------
win.mainloop()

Comments