2017-06-07 23 views
3

我有一个程序,使用tkinter和openpyxl来做一个excel比较程序。这里是我的代码:Tkinter和openpyxl错误没有这样的文件或目录''

import openpyxl, csv 
from tkinter import * 
from tkinter.filedialog import askopenfilename 
from openpyxl.utils import get_column_letter, column_index_from_string 

output = open('differences.csv', 'w', newline='') 
output_writer = csv.writer(output) 

wb1, wb2 = '', '' 
sheet1, sheet2 = '', '' 
column_1, column_2 = '', '' 

root = Tk() 
root.configure(background='light green') 
root.geometry("500x500") 
root.wm_title("BananaCell") 

e1 = Text(root, width=15, height=1) 
e1.pack() 
e1.place(x=70, y=150) 

e2 = Text(root, width=15, height=1) 
e2.pack() 
e2.place(x=300, y=150) 

column1_entry = Text(root, width=5, height=1) 
column1_entry.pack() 
column1_entry.place(x=135, y=250) 

column2_entry = Text(root, width=5, height=1) 
column2_entry.pack() 
column2_entry.place(x=385, y=250) 


def destroy(): 
    root.destroy() 


def ask_for_filename_1(): 
    global wb1 
    wb1 = askopenfilename(title="Select Workbook 1") 
    print(str(wb1)) 
    return wb1 


def ask_for_filename_2(): 
    global wb2 
    wb2 = askopenfilename(title="Select Workbook 1") 
    print(str(wb2)) 
    return wb2 


def ask_for_sheet1(): 
    global sheet1 
    sheet1 = e1.get("1.0", "end-1c") 
    print(sheet1) 
    return sheet1 


def ask_for_sheet2(): 
    global sheet2 
    sheet2 = e2.get("1.0", "end-1c") 
    print(sheet2) 
    return sheet2 


def get_col_1(): 
    global column_1 
    c1 = column1_entry.get("1.0", "end-1c") 
    print(c1) 


def get_col_2(): 
    global column_2 
    c2 = column2_entry.get("1.0", "end-1c") 
    print(c2) 

filename_button1 = Button(root, text="Workbook 1", width=12, height=2, command=ask_for_filename_1) 
filename_button1.pack() 
filename_button1.place(x=100, y=100) 

filename_button2 = Button(root, text="Workbook 2", width=12, height=2, 
command=ask_for_filename_2) 
filename_button2.pack() 
filename_button2.place(x=300, y=100) 

col_button1 = Button(root, text="Enter", width=5, height=1, command=get_col_1) 
col_button1.pack() 
col_button1.place(x=185, y=248) 

col_button2 = Button(root, text="Enter", width=5, height=1, command=get_col_2) 
col_button2.pack() 
col_button2.place(x=435, y=248) 

col1 = column_1 
col2 = column_2 

sheet_button1 = Button(root, text="Enter", width=6, height=0, 
command=ask_for_sheet1) 
sheet_button1.pack() 
sheet_button1.place(x=15, y=147) 

sheet_button2 = Button(root, text="Enter", width=6, height=0, command=ask_for_sheet2) 
sheet_button2.pack() 
sheet_button2.place(x=430, y=147) 

label1 = Label(root, text="Sheet 1 column letter: ", bg="light green") 
label1.pack() 
label1.place(x=10, y=250) 

label2 = Label(root, text="Sheet 2 column letter: ", bg = "light green") 
label2.pack() 
label2.place(x=260, y=250) 


workbook1 = openpyxl.load_workbook(str(wb1)) 
workbook2 = openpyxl.load_workbook(str(wb2)) 

worksheet1 = workbook1.get_sheet_by_name(str(sheet1)) 
worksheet2 = workbook2.get_sheet_by_name(str(sheet2)) 

col1 = column_1 
col2 = column_2 


def show(): 
    scrollbar = Scrollbar(root) 
    scrollbar.pack(side=RIGHT, fill=Y) 
    textbox = Text(root, wrap=WORD, yscrollcommand=scrollbar.set) 
    textbox.pack() 
    textbox.place(x=300, y=200) 
    for (col, col_1) in zip(worksheet1.iter_cols(min_col = column_index_from_string(col1), max_col=column_index_from_string(col1)), worksheet2.iter_cols(min_col = column_index_from_string(col2), max_col=column_index_from_string(col2))): 
     for (cell, cell_1) in zip(col, col_1): 
      if cell.value != cell_1.value and cell.row == cell_1.row: 
       textbox.insert(INSERT, 'Row ' + str(cell.row) + ' ' + 
str(cell.value) + ' is not equal to ' + str(cell_1.value) + ' ' + 'Row ' + str(cell_1.row) + '\n') 


def write_csv(): 
    for (col, col_1) in zip(worksheet1.iter_cols(min_col = column_index_from_string(col1), max_col=column_index_from_string(col1)), worksheet2.iter_cols(min_col = column_index_from_string(col2), max_col=column_index_from_string(col2))): 
     for (cell, cell_1) in zip(col, col_1): 
      if cell.value != cell_1.value and cell.row == cell_1.row: 
       output_writer.writerow(['Sheet 1 value: ' + ' ' + str(cell.value) + ' ' + 'is not equal to ' + ' ' + 'Sheet 2 value: ' + ' ' + str(cell_1.value) + ' ' + 'on row ' + ' ' + str(cell.row)]) 

show_button = Button(root, text="Show", width=8, height=1, command=show) 
show_button.pack() 
show_button.place(x=1, y=20) 

button_export = Button(root, text="Export to CSV", width=10, height=1, command=write_csv) 
button_export.pack() 
button_export.place(x=450, y=60) 

dButton = Button(root, text="Done", width=8, height=1, command=destroy) 
dButton.pack() 
dButton.place(x=100, y=60) 

mainloop() 

程序崩溃,并给了我以下错误:No such file or directory '',我不知道它会完全运行按计划进行。任何人都可以重写我的代码,使其顺利编译?任何事情都很好

+0

任何人都可以帮我吗? – Bill

+2

请创建一个[mcve]。你已经发布了很多代码,看起来不是问题的一部分。另外,你为什么要调用'quit',而后者试图创建更多的小部件?在调用'mainloop'后,他非常不寻常地拥有更多的GUI代码。 –

+0

@BryanOakley我澄清了我的答案任何人都可以请帮助我有一个截止日期为6月15日请别人! – Bill

回答

1

从我所看到的代码中,问题是因为您实际上没有将文件名添加到您尝试使用的变量中。

所以,当你的程序得到的地方workbook1workbook2worksheet1,并且worksheet2是程序正试图变量wb1wb2wb1sheet1之前运行的命令openpyxl.load_workbook(str(wb1)),并sheet2已创建的线条。你需要有一个要求的文件名分配给变量调用的函数创建以下之前:

workbook1 = openpyxl.load_workbook(str(wb1)) 
workbook2 = openpyxl.load_workbook(str(wb2)) 

worksheet1 = workbook1.get_sheet_by_name(str(sheet1)) 
worksheet2 = workbook2.get_sheet_by_name(str(sheet2)) 

这里是你可能想在为了做到这一点做的一个例子。

这不是最好的方法,或者甚至是一种好方法,但它可以满足您的急需需求,并举例说明您的工作簿和工作表需要妥善处理的内容。

def ask_for_filename_1(): 
    global wb1 
    wb1 = askopenfilename(title="Select Workbook 1") 
    print(str(wb1)) 
    return wb1 

ask_for_filename_1() # add this line 

def ask_for_filename_2(): 
    global wb2 
    wb2 = askopenfilename(title="Select Workbook 1") 
    print(str(wb2)) 
    return wb2 

ask_for_filename_2() # add this line 

,你需要为你工作之前与所述变量应用于文件名的变量函数永远做到这一点。

记住我给的例子只是为了说明什么需要你做的变量workbook1workbook2worksheet1,并且worksheet2任何事情之前发生。您可能需要查看和更改代码,以便将来不会出现问题。

也许这部分添加:

workbook1 = openpyxl.load_workbook(str(wb1)) 
workbook2 = openpyxl.load_workbook(str(wb2)) 

worksheet1 = workbook1.get_sheet_by_name(str(sheet1)) 
worksheet2 = workbook2.get_sheet_by_name(str(sheet2)) 

的是每个wb1wb2wb1sheet1后调用,并sheet2已创建功能。

+0

非常明确的答案谢谢 – Bill

+0

如果这个答案是有帮助的考虑应用赏金这个答案:D –

相关问题