2

I have a python program that merges 'n' number of excel files where 'n' is asked from the user. I have the python program made but i don't know how to make an interface where it asks the user to select the files that the user wants to merge and the output destination. Any kind of help would be highly appreciated.

2

Assuming that you are looking for a GUI front end I personally would recommend wxPython.

  • Free, Gratis & Open Source
  • Simple Licence including commercial use
  • Native Look & Feel
  • Cross Platform
  • Great Demo with reusable sample code

Simply pip install wxpython then once the installation is finished run wxdemo to download, install and run the demo for the installed version (download and install is once per installation).

The you can look at the file dialogue demo and its code to get a very good start. Demo FileDialog You can even modify the demo code in place to try things out.

wxdocs will similarly download, install & open in your browser the documentation for your version of wxPython.

Disclaimer: I am the original author of the wxDemo utility

As @Mawg mentioned in the comments if you only need a command line interface argparse (Official HowTo) is the way to go. One trick that I like to do with my python code is to combine wxPython with argparse so that if the user supplies all of the required parameters on the command line it just works but if there are none, or a --gui option is used then the wxPython is called.

If the user is expected to select multiple Excel files some code such as:

import wx
import os

APP = None  # Global wx Application - you are required to have exactly one

def gui_get_files():
    """ Use a wx GUI to prompt the user for the file(s) """
    global APP
    if APP is None:                                                                            
        APP = wx.App()                                                                         
    filelist = []                                                                              
    wcard = "Excel Files|*.xlsx|Old Files|*.xls|CSV|*.csv" # Excel Wildcards
    msg = f"Select files to add, if you have finished adding click cancel"                     
    dlg = wx.FileDialog(None,                                                                  
        message=msg,                                                                           
        defaultDir=os.getcwd(),                                                                
        defaultFile="",                                                                        
        wildcard=wcard,
        style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST | wx.FD_PREVIEW  
        )                                                                                      
    result = wx.ID_OK                                                                          
    while result == wx.ID_OK:                                                                  
        result = dlg.ShowModal()                                                               
        if result == wx.ID_OK:                                                                 
            filelist.extend(dlg.GetPaths())                                                    
    return filelist                                                                            

print(gui_get_files()) # Just to test this out

Demo Appears several times and on cancel after picking several files returns: ['C:\\Users\\Gadget\\Documents\\No_Country.xlsx', 'C:\\Users\\Gadget\\Documents\\PUGGE.xlsx', 'C:\\Users\\Gadget\\Documents\\PUGGE_Members.xlsx']

  • 1
    Thanks for your help. This is something i was looking for. Can you share some tutorial link so that i can get started with ease? – Amreeta Koner Jun 25 at 6:59
  • 2
    For a GUI,. I 100% agree, Steve (and upvote), but this is so trivial that a new coder might be better off withotu a GUI and just using argParse – Mawg Jun 25 at 11:55
  • 1
    @Mawg Agree, this kind of thing is perfect for command line argument parsing – jrw32982 supports Monica Jun 25 at 18:08
  • Added mention of argparse and some demo code. – Steve Barnes Jun 26 at 5:38
  • 1
    This was so ideal @SteveBarnes, thank you with both wxpython and the code... it just saved me from troubles – Amreeta Koner Jun 27 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.