python-如何使这个变量从我得到的路径读取excel文件?



有两个按钮"浏览文件"浏览文件"打开并读取选定的excel文件路径,如何读取文件路径("open"这些excel文件,使我的其余代码在save_slogan()工作)由变量df1和df2 ??查看我的函数save_slogan(),这里是我的代码:

from tkinter import *
# import filedialog module
from tkinter import filedialog
import pandas as pd
# Function for opening the
# file explorer window
from tkinter import *
import smtplib
def browseFiles1():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)

def browseFiles():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)

def save_slogan():
import pandas as pd
df1=#read and open the excel file from its file path from function browseFiles() 
df2=#read and open the excel file from its file path from function browseFiles1()
xx1=df1.rename(columns={"Description":"Desc 2A","Doc Number":"Document Number"})
xx=df2.rename(columns={"Description":"Desc PR","Doc Number":"Document Number"})
combined = pd.merge(xx1, xx, how="outer", on="Document Number")
def case_code(row): 
if row["Tax Amount 2A"] == row["Tax Amount PR"]:
return "exact"
elif pd.isna(row["Tax Amount 2A"]):
return "Addition in PR"
elif pd.isna(row["Tax Amount PR"]):
return "Addition in 2A"
elif row["Tax Amount 2A"] != row["Tax Amount PR"]:
return "mismatch"
codes=combined.apply(case_code, axis="columns")
answer = combined.assign(**{"Match type": codes})
final=answer[["Match type"] + [*combined.columns]] 
final.to_excel('done1.xlsx')     

# Create the root window
window = Tk()
# Set window title
window.title('File Explorer')
# Set window size
window.geometry("500x500")
#Set window background color
window.config(background = "white")
# Create a File Explorer label
label_file_explorer = Label(window,
text = "PR2A",
width = 100, height = 4,
fg = "blue")

button_explore = Button(window,
text = "Browse File 2A",
command = browseFiles)
button_explore1 = Button(window,
text = "Browse File PR",
command = browseFiles1)
button_explore2 = Button(window,
text = "Go and Save",
command = save_slogan)


# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
button_explore1.grid(column=1, row=3)
button_explore2.grid(column=1,row=4)

# Let the window wait for any events
window.mainloop()

请帮助

需要存储选中的文件。因为您需要知道所选文件是否用于"2A"或"PR",建议使用字典保存所选文件。

您可以将两个browseFiles函数合并为一个,并将所需的文件类型作为参数传递:

# dictionary to store the selected files
selected_files = {"2A": None, "PR": None}
def browseFile(ftype):
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Excel files",
"*.xlsx"),
("all files",
"*.*")))
if filename:
# a file is selected, so store the selected file
selected_files[ftype] = filename
# Change label contents
txt = "n".join(f"{ftype}: {fname}" for ftype, fname in selected_files.items())
label_file_explorer.configure(text="File Opened:n"+txt)
def save_slogan():
import pandas as pd
# check whether required files are selected
if all(selected_files.values()):
df1=pd.read_excel(selected_files["2A"])
df2=pd.read_excel(selected_files["PR"])
xx1=df1.rename(columns={"Description":"Desc 2A","Doc Number":"Document Number"})
xx=df2.rename(columns={"Description":"Desc PR","Doc Number":"Document Number"})
combined = pd.merge(xx1, xx, how="outer", on="Document Number")
def case_code(row):
if row["Tax Amount 2A"] == row["Tax Amount PR"]:
return "exact"
elif pd.isna(row["Tax Amount 2A"]):
return "Addition in PR"
elif pd.isna(row["Tax Amount PR"]):
return "Addition in 2A"
elif row["Tax Amount 2A"] != row["Tax Amount PR"]:
return "mismatch"
codes=combined.apply(case_code, axis="columns")
answer = combined.assign(**{"Match type": codes})
final=answer[["Match type"] + [*combined.columns]]
final.to_excel('done1.xlsx')

然后改变两个按钮的command选项如下:

button_explore = Button(window,
text = "Browse File 2A",
command = lambda: browseFile("2A"))
button_explore1 = Button(window,
text = "Browse File PR",
command = lambda: browseFile("PR"))

相关内容

  • 没有找到相关文章

最新更新