-1

Hi i made a python script to copy values from one excel file to another but now i need to prompt the user so that they can select the files from their file explorer without changing the path in the script itself what is the best way to do this ? the people that are going to work with it also don't really know alot about scripts what is the best dummy proof way to just let them run it ?

My code is the following:

import openpyxl as xl
from openpyxl.styles import Alignment, Font

# opening the source excel file
filename = "C:\\excelProjects\\Test Meg Vanhoef.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]`
  
# opening the destination excel file 
filename1 = "C:\\excelProjects\\2223 Aanstipschema EGS v061221 - met koppeling forms.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.active
  
# calculate total number of rows in source excel file
mr = ws1.max_row

for i in range(2, mr+1):
    #B
    ws2['B{}'.format(i)] = ws1.cell(row=i, column=12).value
    #C
    ws2['C{}'.format(i)] = ws1.cell(row=i, column=13).value
    #D
    ws2['D{}'.format(i)] = ws1.cell(row=i, column=17).value
    #E
    ws2['E{}'.format(i)] = ws1.cell(row=i, column=15).value
    #F
    if ws1.cell(row=i, column=21).value == 'Andere':
        ws2['F{}'.format(i)] = ws1.cell(row=i, column=22).value
    else:
        ws2['F{}'.format(i)] = ws1.cell(row=i, column=21).value
    #G
    Y_val = ws1.cell(row=i, column=25).value
    Z_val = ws1.cell(row=i, column=26).value
    ws2['G{}'.format(i)] = f"{Y_val} {Z_val}"

    #H
    ws2.cell(row=i, column=8).value = ws1.cell(row=i, column=27).value
    #I
    AG_val = ws1.cell(row=i, column=33).value
    AB_val = ws1.cell(row=i, column=28).value
    AC_val = ws1.cell(row=i, column=29).value
    AN_val = ws1.cell(row=i, column=40).value
    AH_val = ws1.cell(row=i, column=34).value
    AJ_val = ws1.cell(row=i, column=36).value

    ws2['I{}'.format(i)] = f"{AG_val} ({AB_val}, {AC_val}) \n{AN_val} ({AH_val}, {AJ_val})"
    ws2['I{}'.format(i)].alignment = Alignment(wrapText=True)
    #J
    AF_val = ws1.cell(row=i, column=32).value
    ws1.cell(row=i, column=39).font = Font(bold=True)
    AM_val = ws1.cell(row=i, column=39).value

    ws2['J{}'.format(i)] = f'{AF_val} ({AB_val}) \n{AM_val} ({AH_val})'
    ws2['J{}'.format(i)].alignment = Alignment(wrapText=True)
    #K
    ws2['K{}'.format(i)] = ws1.cell(row=i, column=50).value
    #L
    ws2['L{}'.format(i)] = ws1.cell(row=i, column=51).value
    #M
    ws2['M{}'.format(i)] = ws1.cell(row=i, column=16).value
    #N
    ws2['N{}'.format(i)] = ws1.cell(row=i, column=41).value
    #O
    ws2['O{}'.format(i)] = ws1.cell(row=i, column=47).value
    #P
    ws2['P{}'.format(i)] = ws1.cell(row=i, column=49).value
    #Q
    ws2['Q{}'.format(i)] = ws1.cell(row=i, column=52).value
    #S
    BA_val = ws1.cell(row=i, column=53).value
    BB_val = ws1.cell(row=i, column=54).value
    CS_val = ws1.cell(row=i, column=96).value
    CT_val = ws1.cell(row=i, column=97).value
    if CS_val != 'Nederlands' and CT_val != 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met moeder: {CS_val} \nTaal met vader: {CT_val}'
    elif CT_val == 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met vader: {CT_val}'
    elif CS_val == 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met moeder: {CS_val}'
    else:
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val}'
    ws2['S{}'.format(i)].alignment = Alignment(wrapText=True)
    #W
    BG_val = ws1.cell(row=i, column=59).value
    BH_val = ws1.cell(row=i, column=60).value
    BI_val = ws1.cell(row=i, column=61).value
    BJ_val = ws1.cell(row=i, column=62).value
    BK_val = ws1.cell(row=i, column=63).value
    BL_val = ws1.cell(row=i, column=64).value
    BM_val = ws1.cell(row=i, column=65).value

    if BK_val == None and BM_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \n{BL_val}'
    elif BM_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \nmedicatie: {BK_val} \n{BL_val}'
    elif BK_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \n{BL_val} \nverwachtingen school: {BM_val}'
    else:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \nmedicatie: {BK_val} \n{BL_val} \nverwachtingen school: {BM_val}'
    ws2['W{}'.format(i)].alignment = Alignment(wrapText=True)

    #AS
    BO_val = ws1.cell(row=i, column=67).value
    BP_val = ws1.cell(row=i, column=68).value
    BR_val = ws1.cell(row=i, column=70).value

    ws2['AS{}'.format(i)] = f'Hobby / talenten: {BO_val} / {BR_val} \nInteresses: {BP_val}'
    ws2['AS{}'.format(i)].alignment = Alignment(wrapText=True)

    #AU
    BS_val = ws1.cell(row=i, column=71).value
    BT_val = ws1.cell(row=i, column=72).value
    BV_val = ws1.cell(row=i, column=74).value
    BX_val = ws1.cell(row=i, column=76).value
    BZ_val = ws1.cell(row=i, column=78).value
    CB_val = ws1.cell(row=i, column=80).value
    CD_val = ws1.cell(row=i, column=82).value
    CE_val = ws1.cell(row=i, column=83).value

    ws2['AU{}'.format(i)] = f'Talenten: {BS_val} / {BV_val} / {BT_val} \nLeermoeilijkheden: {BX_val} / {BZ_val} \nVroegere begeleiding: {CB_val} / {CD_val} \nBelemmerende factoren: {CE_val}'
    ws2['AU{}'.format(i)].alignment = Alignment(wrapText=True)

    #AW
    CF_val = ws1.cell(row=i, column=84).value
    AO_val = ws1.cell(row=i, column=41).value
    CJ_val = ws1.cell(row=i, column=88).value
    CL_val = ws1.cell(row=i, column=90).value
    CN_val = ws1.cell(row=i, column=92).value
    CO_val = ws1.cell(row=i, column=93).value
    CP_val = ws1.cell(row=i, column=94).value
    CQ_val = ws1.cell(row=i, column=95).value

    if CO_val == 'ja':
        ws2['AW{}'.format(i)] = f"Karakter: {CF_val} \nMedisch: {BG_val}, {BH_val}, {BL_val}, {BM_val} \nFamiliaal: {AO_val} \nMoeilijkheden: {CI_val} / {CK_val} \nVroegere ondersteuning: {CM_val} / {CO_val} \nBelemmerende factoren: Indien \npestverleden \n{CQ_val} {CR_val}"
    else:
        ws2['AW{}'.format(i)] = f"Karakter: {CF_val} \nMedisch: {BG_val}, {BH_val}, {BL_val}, {BM_val} \nFamiliaal: {AO_val} \nMoeilijkheden: {CI_val} / {CK_val} \nVroegere ondersteuning: {CM_val} / {CO_val} \nBelemmerende factoren: Indien \n{CQ_val} {CR_val}"
    ws2['AW{}'.format(i)].alignment = Alignment(wrapText=True)

# saving the destination excel file
wb2.save(str(filename1))
  • 1
    Have you looked at `tkinter.filedialog`? As for a 'dummy proof' way of running a Python script, assuming everyone just has a recent version of Python installed on their system, kept up to date by your IT department, running a script could just be a matter of double-clicking it if it is associated with the `python.exe` executable, but that's a very risky setup. It would be better to install the script with a batch file that runs it in a correct environment. – Grismar Dec 23 '21 at 07:50
  • Welcome to Stack Overflow. Please read [ask] and https://stackoverflow.com/help/dont-ask, and make sure you understand that this is *not a discussion forum*. We do not do program design here and we do not deal in "best ways" or UI/UX choices. – Karl Knechtel Dec 23 '21 at 07:50

3 Answers3

1
import os 
os.getcwd() 

returns current working path. You can use it as a variable to output path.

You should ask users of the script to have the source Excel file in the same path as your .py file;

If I understood you correctly, this should help.

Niqua
  • 386
  • 2
  • 15
0

If you think the users are going to be non-technical, you can use a tkinter window to choose the files. For eg, the below code gets the 2 file paths, and then you can work with it.

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

source_file_path = filedialog.askopenfilename(title="Choose source file")
target_file_path = filedialog.askopenfilename(title="Choose target file")

There are options, to filter file types, starter directory etc, for that you can refer the documentation.

Kris
  • 8,680
  • 4
  • 39
  • 67
0

I would use tkinter.filedialog for this task, consider following simple example

import tkinter as tk
from tkinter import filedialog
filepath1 = ""
def callback():
    global filepath1
    filepath1 = filedialog.askopenfilename()
    root.destroy()
root = tk.Tk()
btn = tk.Button(root, text="Click to select file", command=callback)
btn.pack()
tk.mainloop()
print("User selected", filepath1)

If you are allowed to use external modules as you wish, then you might use easygui as follows

import easygui
filepath = easygui.fileopenbox("Select file")
print("User selected", filepath)

In order to install easygui just do pip install easygui

Daweo
  • 31,313
  • 3
  • 12
  • 25