How to install a exe script in python?

  python-3.x, tkinter

I am making an audiobook and to listen to the text of an image I tried to use tesseract-OCR and it worked but when I will share my application with somebody else I don’t whether he has installed tesseract-OCR or not so I wanted that when my script starts it automatically installs the tesseract-OCR but how will I do it please someone help me.

import os
import tkinter
from tkinter import *
from tkinter import filedialog as fd
import PyPDF2
from pyttsx3 import *
from PyPDF2 import *
from docx import Document
import pytesseract
import cv2
import pyttsx3
from pygame import mixer
from tkinter import messagebox as md

# Setting Up the tkinter window


win = Tk()
win.geometry('550x750')
win.config(background='#d4fffe')
win.title('AudioBook')
icon=PhotoImage(file='audiobook.png')
win.iconphoto(False,icon)
# Set Up Complete

# Setting Up and co

vari=StringVar()

# Setting up complete

# Creating functionality

def select_file():
    global file
    if vari.get() == 'Word Document':
        file=fd.askopenfilename(initialdir='/',filetypes=[('Word FILE','*.docx')])
    elif vari.get()== 'Pdf Document':
        file=fd.askopenfilename(initialdir='/',filetypes=[('PDF FILE','*.pdf')])
    elif vari.get() == 'Image(Indiviual)' :
        file=fd.askopenfilename(initialdir='/',filetypes=[('JPG','*jpg'),('PNG','*png')])
    elif vari.get()=='Text Document':
        file=fd.askopenfilename(initialdir='/',filetypes=[('Text File','*.txt')])
    
    button2.config(state=NORMAL,fg='MidnightBlue')

def check(*args):
    if vari.get():
        button.config(state=NORMAL,fg='MidnightBlue')
    else:
        button.config(state=DISABLED)


def ocr():
    global word,pdfs,image1,txt,text_word,text_img,text_txt,pdf_txt
    word=FALSE
    pdfs=FALSE
    image1=FALSE
    txt=FALSE
    if file:
        if file.endswith('.docx'):
            word=TRUE
        elif file.endswith('.pdf'):
            pdfs=TRUE
        elif file.endswith('.PNG') or file.endswith('.jpg'):
            image1=TRUE
        elif file.endswith('.txt'):
            txt=TRUE

    if word:
        text_word1=[]
        doc=Document(file)
        for par in doc.paragraphs:
            text_word1.append(par.text)

        text_word=''.join(text_word1)
        box.delete('1.0',END)
        box.insert(tkinter.END,text_word)

    elif pdfs:
        try:
            pdf_reader=PyPDF2.PdfFileReader(file)
            for pages in range(pdf_reader.numPages):
                page=pdf_reader.getPage(pages)
                pdf_txt=page.extractText()
                box.delete('1.0',END)
                box.insert(tkinter.END,pdf_txt)
        except:
            box.delete('1.0',END)
            box.insert(tkinter.END,"Text Can't Be Recognised In PDF File Try Image Option")

    elif image1:
        try:
            pytesseract.pytesseract.tesseract_cmd=r'C:/Program Files (x86)/Tesseract- OCR/tesseract.exe'
            img=cv2.imread(file)
            imgf=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
            text_img=pytesseract.image_to_string(imgf)
            box.delete('1.0',END)
            box.insert(tkinter.END,text_img)
        except:
            box.delete('1.0',END)
            box.insert(tkinter.END,"Text Can't be Reconginsed in Image")
    elif txt:
        txt1=open(file,'r')
        text_txt=txt1.read()
        box.delete('1.0',END)
        box.insert(tkinter.END,text_txt)

    button3.config(state=NORMAL,fg='MidnightBlue')

def play():
    direc=os.getcwd()
    new_dir=direc.replace('','/')
    mixer.init()
    engine=pyttsx3.init()
    engine.setProperty('rate',165)
    if word:
        if os.path.isfile(new_dir+'/'+'my.wav'):
            engine.save_to_file(text_word,new_dir+'/'+'my2.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my.wav')
        elif os.path.isfile(new_dir+'/'+'my2.wav') :
            engine.save_to_file(text_word,new_dir+'/'+'my.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my2.wav')
        else:
            engine.save_to_file(text_word,new_dir+'/'+'my.wav')
            engine.runAndWait()
    elif pdfs:
        if os.path.isfile(new_dir+'/'+'my.wav'):
            engine.save_to_file(pdf_txt,new_dir+'/'+'my2.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my.wav')
        elif os.path.isfile(new_dir+'/'+'my2.wav'):
            engine.save_to_file(pdf_txt,new_dir+'/'+'my.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my2.wav')
        else:
            engine.save_to_file(pdf_txt,new_dir+'/'+'my.wav')
            engine.runAndWait()
    elif image1:
        if os.path.isfile(new_dir+'/'+'my.wav'):

            engine.save_to_file(text_img,new_dir+'/'+'my2.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my.wav')
        elif os.path.isfile(new_dir+'/'+'my2.wav'):
            engine.save_to_file(text_img,new_dir+'/'+'my.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my2.wav')
        else:
            engine.save_to_file(text_img,new_dir+'/'+'my.wav')
            engine.runAndWait()

    elif txt:
        if os.path.isfile(new_dir+'/'+'my.wav'):

            engine.save_to_file(text_txt,new_dir+'/'+'my2.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my.wav')
        elif os.path.isfile(new_dir+'/'+'my2.wav'):
            engine.save_to_file(text_txt,new_dir+'/'+'my.wav')
            engine.runAndWait()
            mixer.music.unload()
            os.remove(new_dir+'/'+'my2.wav')
        else:
            engine.save_to_file(text_txt,new_dir+'/'+'my.wav')
            engine.runAndWait()




    if os.path.isfile(new_dir+'/'+'my.wav'):
        mixer.music.load(new_dir+'/'+'my.wav')
        mixer.music.play()
    else:
        mixer.music.load(new_dir+'/'+'my2.wav')
        mixer.music.play()

    button4.config(state=NORMAL,fg='MidnightBlue')
    button5.config(state=NORMAL,fg='MidnightBlue')
    button6.config(state=NORMAL,fg='MidnightBlue')
    
def pause():
    mixer.music.pause()

def unpause():
    mixer.music.unpause()

def stop():
    mixer.music.stop()
    

# Complete

# Creating widgets
labl1=Label(win,text='Welcome To AudioBook',bg='#d4fffe',font=('Consolas',13,'bold')).place(x=190,y=20)
labl=Label(win,text='Select File Type To Listen Text',bg='#d4fffe',font=('Consolas',13,'bold')).place(x=140,y=60)

drop=OptionMenu(win,vari,'Select File Type ','Word Document','Pdf Document','Image(Indiviual)','Text Document',command=check)

button=Button(win,text='Select File',command=select_file,state=DISABLED,width=15,height=2,bg='LightSkyBlue')

button2=Button(win,text='Extract Text',command=ocr,state=DISABLED,width=15,height=2,bg='LightSkyBlue')

button3=Button(win,text='Listen Text',command=play,state=DISABLED,width=15,height=2,bg='LightSkyBlue')

button4=Button(win,text='Pause',command=pause,state=DISABLED,width=15,height=2,bg='LightSkyBlue')

button5=Button(win,text='Resume',command=unpause,state=DISABLED,width=15,height=2,bg='LightSkyBlue')

button6=Button(win,text='Stop Listening',command=stop,state=DISABLED,width=15,height=2,bg='LightSkyBlue')


box=Text(win,width=60,height=15,font=('consolas',11,'italic'))
#placement

drop.place(x=205,y=120)
button.place(x=208,y=170)
box.place(x=35,y=240)
button2.place(x=110,y=530)
button3.place(x=310,y=530)
button4.place(x=110,y=600)
button5.place(x=310,y=600)
button6.place(x=208,y=670)
#designing

drop.config(width=15,bg='LightSkyBlue',highlightbackground='LightSkyBlue')
button2.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
button.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
button3.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
button4.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
button5.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
button6.config(font=('Consolas',11,'italic'),activebackground='cornsilk')
drop["menu"]["background"] = 'lightskyblue'
drop["menu"]["foreground"] = 'navy'
drop["menu"]["font"] = ('Consolas',11,'italic')
drop['height']= 1

# Creation complete

win.mainloop()


Source: Python-3x Questions

LEAVE A COMMENT