如何读取从用户输入路径定义的excel文件

2024-05-16 00:53:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个代码,一个是“OK_Separating_testing_files.py”,另一个是“My_GUI.py”

经过多次尝试,我终于可以让他们一起工作了

首先,我有我的GUI“my_GUI.py”

#~~~~~~~~~~~~~~~~~~ Import required modules ~~~~~~~~~~~~~~~~~~#
    from tkinter import messagebox, filedialog
    from tkinter import *
    from tkinter.ttk import *
    import tkinter as tk
    import Creating_testing_files
    import subprocess
    
    class ParentWindow(tk.Frame):
        def __init__(self, master, *args, **kwargs):
            Frame.__init__(self, master, *args, **kwargs)
            self.master = master
            self.master.maxsize(930, 450)
            self.master.minsize(930, 450)
            self.master.title("My Testing Tool")
            arg = self.master
            b = StringVar()
    
            #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Browse directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
            def select_file():
                my_file = filedialog.askopenfilename(initialdir=".", filetypes=[('Excel', ('*.xls', '*.xlsm', '*.xlsx')), ('CSV', '*.csv',)])
                print(self.lab_file.insert(END, my_file))
    
            self.lab_file = tk.Entry(self.master, textvariable=b, width=126, borderwidth=2, relief=tk.GROOVE)
            self.lab_file.grid(row=2, column=1, sticky="EW")
    
            self.btn_browse = tk.Button(self.master, highlightbackground='#333', text="Browse", command=select_file, width=8, font=("Arial", 9))
            self.btn_browse.grid(row=2, column=0, padx=(65,0))
    
            #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Run button and message box ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
            self.lab_run = tk.Label(self.master, font=("Arial", 10), text="Start the preparation: ")
            self.lab_run.grid(row=9, column=0, sticky="E")
    
            def run_script():
                my_script = ['python', 'Creating_testing_files.py']
                result1 = subprocess.Popen(my_script, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
                out1, err1 = result1.communicate()
                status_wrapper = out1.decode("utf-8")
                messagebox.showinfo("Processing", "Testing files are being processed!\n\nPlease close the tool's window.")
    
            self.btn_run = tk.Button(self.master, highlightbackground='#333', text="Run", command=run_script, width=8, font=("Arial", 9))
            self.btn_run.grid(row=9, column=1, sticky="NW")
    
    def main():
        main = tk.Tk()
        app = ParentWindow(main)
        main.mainloop()
    
    #-------------------- Reference with main script --------------------#
    def service_func():
        print("")
    
    if __name__ == '__main__':
        service_func()
        Creating_testing_files.some_func()

然后我有了我的主脚本“OK_Separating_testing_files.py”,它将在用户选择输入Excel文件后执行

#-------------------- Libraries --------------------#
import openpyxl
from openpyxl.worksheet.datavalidation import DataValidation
import My_GUI
My_GUI.main()

#-------------------- Opening network LLD file --------------------#
wb = openpyxl.load_workbook("HERE MUST BE THE USER INPUT FILE NAME")

#-------------------- Worksheets from network LLD file --------------------#
ws_conn = wb["sheet1"]
ws_old_fqdn = wb["FQDN"]

#-------------------- New files details --------------------#
    #----- FQDNs file -----#
wb_fqdn = openpyxl.Workbook()
ws_fqdn_filter = wb_fqdn.create_sheet("Sheet", 1)
ws_fqdn_filter.title = 'Filters'
ws_fqdn = wb_fqdn['Sheet']
ws_fqdn.title = 'FQDNs'

#-------------------- Copying FQDNs values --------------------#
for row in ws_old_fqdn.rows:
    for cell in row:
        ws_fqdn[cell.coordinate] = cell.value
        fqdn_cell = ws_fqdn[cell.coordinate]

#-------------------- Reference with GUI --------------------#
def some_func():
    print("")

if __name__ == '__main__':
    some_func()

我试图做的是使用“My_GUI.py”代码中的用户输入Excel文件作为工作簿,我的“OK_testing_files.py”将在该行中处理

wb = openpyxl.load_workbook("HERE MUST BE THE USER INPUT EXCEL FILE NAME")

此时,我所做的是手动更改工作簿的名称以使其工作,但我需要它是用户输入的任何工作簿,而不是让用户更改代码

我希望这个解释足够好

非常感谢


Tags: pyimportselfmasterwsmaindefgui