RawalD
RawalD

Reputation: 371

Adding additional files file into Python executable

I have a script that invokes Selenium and files in values from a selected CSV. I've used PyGUI as the interface. Now I'd like to convert the script into an executable but I also need to add the chromedriver.exe file bundled with it, how do I go about this ? I've tried auto-py-to-exe one file and added the additional file but nothing happens after I launch the app and submit, in which case it needs to launch Chrome and feed values from the CSV. I need to be able to bundle the chromedriver.exe into this program. In folder mode its working perfectly, need to just make it into one-file. My program:

import csv
import time
import threading
from selenium import webdriver
import PySimpleGUI as sg



def make_window(theme):

    sg.theme(theme)
    menu_def = [['&Application', ['E&xit']],
                ['&Help', ['&About']] ]
    right_click_menu_def = [[], ['Exit']]

    # Table Data


    input_layout =  [[sg.Menu(menu_def, key='-MENU-')],
                 
                
                [sg.Button("Open File")],
               
                [sg.Text('Chrome Version')],

                [sg.OptionMenu(values=('96', '97', '98'),  k='-OPTION MENU-'),],
               
                [sg.Button('Submit')]]



    
    
    layout = [[sg.Text('Email Automation', size=(38, 1), justification='center', font=("Helvetica", 16), relief=sg.RELIEF_RIDGE, k='-TEXT HEADING-', enable_events=True)]]
    layout +=[[sg.TabGroup([[  sg.Tab('Setup CSV and Chrome Version', input_layout),
                               
                               ]], key='-TAB GROUP-')]]
              
    return sg.Window('', layout, right_click_menu=right_click_menu_def)


def main():
    window = make_window(sg.theme())
    
    # This is an Event Loop 
    while True:
        event, values = window.read(timeout=100)
        # keep an animation running so show things are happening
       
        if event not in (sg.TIMEOUT_EVENT, sg.WIN_CLOSED):
            print('============ Event = ', event, ' ==============')
            print('-------- Values Dictionary (key=value) --------')
            for key in values:
                print(key, ' = ',values[key])
        if event in (None, 'Exit'):
            print("[LOG] Clicked Exit!")
            break
        elif event == 'About':
            print("[LOG] Clicked About!")
            sg.popup('Aucor email automation',
                     'Select CSV file',
                     'Select Chrome Version',
                     'Submit',
                     'Powered By ')
        elif event == 'Popup':
            print("[LOG] Clicked Popup Button!")
            sg.popup("You pressed a button!")
            print("[LOG] Dismissing Popup!")


        elif event == "Open File":
            print("[LOG] Clicked Open File!")
            csv_file_selected = sg.popup_get_file('Choose your file')
            sg.popup("You chose: " + str(folder_or_file))
            print("[LOG] User chose file: " + str(folder_or_file))




def run_selenium(window, file, driver):

    with open(file, 'rt') as csv_file:
        csv_reader = csv.reader(csv_file)
    #-------------------------------------------------------------------------------
    # Web Automation
    driver = webdriver.Chrome(executable_path=driver)
    driver.get('')
  
    
    fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
    lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
    phone_field = driver.find_element_by_xpath('//*[@id="PHONE"]')
    mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
    deposit_field = driver.find_element_by_xpath('//*[@id="DEPOSIT"]')
    submit = driver.find_element_by_xpath('//*[@id="sib-form"]/div[8]/div/button')

    with open(file, 'rt', encoding='utf-8-sig') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=';')
        next(csv_reader)
        for line in csv_reader:
            
            time.sleep(2.5)
            
            fname_field.send_keys(line[10])
            lname_field.send_keys(line[11])
            mail_field.send_keys(line[13])
            phone_field.send_keys(line[16])
            deposit_field.send_keys(line[37])
            submit.click()

    # Not to update GUI in thread, but generate an event which will be processed in event loop.
    window.write_event_value('Done', None)

def main():
    # My GUI
    window = make_window(sg.theme())
    folder_or_file = None
    # Using your path for all the drivers of all versions
    paths = {
        #'96': './chromedriver.exe',
        '96': '.chromedriver.exe',
        
    }

    while True:
        event, values = window.read(timeout=100)
        # keep an animation running so show things are happening

        if event not in (sg.TIMEOUT_EVENT, sg.WIN_CLOSED):
            # print('============ Event = ', event, ' ==============')
            # print('-------- Values Dictionary (key=value) --------')
            for key in values:
               # print(key, ' = ',values[key])
        if event in (None, 'Exit'):
           # print("[LOG] Clicked Exit!")
            break
        elif event == 'About':
           # print("[LOG] Clicked About!")
            sg.popup('email',
                     'Select CSV file',
                     'Select Chrome Version',
                     'Submit',
                     '')
        elif event == 'Popup':
           # print("[LOG] Clicked Popup Button!")
            sg.popup("You pressed a button!")
           # print("[LOG] Dismissing Popup!")
        elif event == "Open File":
            #print("[LOG] Clicked Open File!")
            folder_or_file = sg.popup_get_file('Choose your file')
            # sg.popup("You chose: " + str(folder_or_file))
            #print("[LOG] User chose file: " + str(folder_or_file))
        elif event == 'Submit':
            version = values['-OPTION MENU-']
            if folder_or_file is None or version not in paths:
              #  print("No CSV file selected or wrong Chrome version selected")
                continue
            # Using thread to avoid long time job will block and cause GUI no response
            threading.Thread(target=run_selenium, args=(window, folder_or_file, paths[version])).start()
            # Disable Submit button to prevent submit again when threading
            window['Submit'].update(disabled=True)
          #  print('[LOG] Run Selenium ...')
        elif event == 'Done':
            # Enable Submit button when thread done
            window['Submit'].update(disabled=False)
           # print('[LOG] End Selenium')

    window.close()

main()

However I get the following error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

Upvotes: 0

Views: 1108

Answers (1)

RawalD
RawalD

Reputation: 371

Added all files into a folder and added it as a path along with adding the additional file and boom it worked

Upvotes: 0

Related Questions