openpyxl KeyError 工作表 {0} 不存在
openpyxl KeyError Worksheet {0} does not exist
我知道你可能看过这个标题,但请不要标记为重复,因为我的错误是不同的。我正在制作一个 excel 比较程序与 openpyxl 并使用 tkinter 使 UI 更友好。这是我的代码:
import openpyxl
from tkinter import *
from tkinter.filedialog import askopenfilename
from openpyxl.utils import get_column_letter, column_index_from_string
f1 = ''
f2 = ''
sheet1 = ''
sheet2 = ''
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)
def destroy():
root.destroy()
def get_1():
inputValue = e1.get("1.0", "end-1c")
print(inputValue)
def get_2():
inputValue2 = e2.get("1.0", "end-1c")
print(inputValue2)
bf = Button(root, text="Enter", width=6, height=0, command=get_1)
bf.pack()
bf.place(x=15, y=147)
bf = Button(root, text="Enter", width=6, height=0, command=get_2)
bf.pack()
bf.place(x=430, y=147)
def askForFileName1():
global f1
f1 = askopenfilename(title="Select Workbook 1")
print(str(f1))
def askForFileName2():
global f2
f2 = askopenfilename(title="Select Workbook 2")
print(str(f2))
sheet1 = e1.get("1.0", "end-1c")
sheet2 = e2.get("1.0", "end-1c")
b = Button(root, text="Workbook 1", width=12, height=2, command=askForFileName1)
b.pack()
b.place(x=100, y=100)
b2 = Button(root, text="Workbook 2", width=12, height=2, command=askForFileName2)
b2.pack()
b2.place(x=300, y=100)
mainloop()
wb = openpyxl.load_workbook(str(f1))
wb1 = openpyxl.load_workbook(str(f2))
ws = wb.get_sheet_by_name(str(sheet1))
ws1 = wb1.get_sheet_by_name(str(sheet2))
col1 = input('Column letter from Sheet 1 to compare from: ')
col2 = input('Column letter from Sheet 2 to compare from: ')
for (col, col_1) in zip(ws.iter_cols(min_col = column_index_from_string(col1), max_col=column_index_from_string(col1)), ws1.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:
print('Row ' + str(cell.row) + ' ' + str(cell.value) + ' is not
equal to ' + str(cell_1.value) + ' ' + 'Row ' + str(cell_1.row))
exit_if = input('Press x to exit when you\'re ready: ')
一切正常,直到我到达以下行:ws = wb.get_sheet_by_name(str(sheet1))
。在那一行之后它给了我错误:
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet does not exist.'
谁能解决这个问题?任何帮助将不胜感激。
def get_1():
global sheet1
sheet1 = e1.get("1.0", "end-1c")
print(sheet1)
def get_2():
global sheet2
sheet2 = e2.get("1.0", "end-1c")
print(sheet2)
当您调用以下行时
sheet1 = e1.get("1.0", "end-1c")
sheet2 = e2.get("1.0", "end-1c")
那时候e1和e2里面什么都没有。
所以你必须在文件名之类的函数中调用它。
此外,您应该使用 Entry
小部件来获取输入
from openpyxl import load_workbook
wb = load_workbook('file.xlsx')
print(wb.sheetnames)
for worksheets in wb.sheetnames:
worksheet = wb[worksheets]
print(worksheet)
您需要遍历工作表的名称 (wb.sheetnames),而不是遍历工作表的索引 (wb)
我知道你可能看过这个标题,但请不要标记为重复,因为我的错误是不同的。我正在制作一个 excel 比较程序与 openpyxl 并使用 tkinter 使 UI 更友好。这是我的代码:
import openpyxl
from tkinter import *
from tkinter.filedialog import askopenfilename
from openpyxl.utils import get_column_letter, column_index_from_string
f1 = ''
f2 = ''
sheet1 = ''
sheet2 = ''
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)
def destroy():
root.destroy()
def get_1():
inputValue = e1.get("1.0", "end-1c")
print(inputValue)
def get_2():
inputValue2 = e2.get("1.0", "end-1c")
print(inputValue2)
bf = Button(root, text="Enter", width=6, height=0, command=get_1)
bf.pack()
bf.place(x=15, y=147)
bf = Button(root, text="Enter", width=6, height=0, command=get_2)
bf.pack()
bf.place(x=430, y=147)
def askForFileName1():
global f1
f1 = askopenfilename(title="Select Workbook 1")
print(str(f1))
def askForFileName2():
global f2
f2 = askopenfilename(title="Select Workbook 2")
print(str(f2))
sheet1 = e1.get("1.0", "end-1c")
sheet2 = e2.get("1.0", "end-1c")
b = Button(root, text="Workbook 1", width=12, height=2, command=askForFileName1)
b.pack()
b.place(x=100, y=100)
b2 = Button(root, text="Workbook 2", width=12, height=2, command=askForFileName2)
b2.pack()
b2.place(x=300, y=100)
mainloop()
wb = openpyxl.load_workbook(str(f1))
wb1 = openpyxl.load_workbook(str(f2))
ws = wb.get_sheet_by_name(str(sheet1))
ws1 = wb1.get_sheet_by_name(str(sheet2))
col1 = input('Column letter from Sheet 1 to compare from: ')
col2 = input('Column letter from Sheet 2 to compare from: ')
for (col, col_1) in zip(ws.iter_cols(min_col = column_index_from_string(col1), max_col=column_index_from_string(col1)), ws1.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:
print('Row ' + str(cell.row) + ' ' + str(cell.value) + ' is not
equal to ' + str(cell_1.value) + ' ' + 'Row ' + str(cell_1.row))
exit_if = input('Press x to exit when you\'re ready: ')
一切正常,直到我到达以下行:ws = wb.get_sheet_by_name(str(sheet1))
。在那一行之后它给了我错误:
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet does not exist.'
谁能解决这个问题?任何帮助将不胜感激。
def get_1():
global sheet1
sheet1 = e1.get("1.0", "end-1c")
print(sheet1)
def get_2():
global sheet2
sheet2 = e2.get("1.0", "end-1c")
print(sheet2)
当您调用以下行时
sheet1 = e1.get("1.0", "end-1c")
sheet2 = e2.get("1.0", "end-1c")
那时候e1和e2里面什么都没有。 所以你必须在文件名之类的函数中调用它。
此外,您应该使用 Entry
小部件来获取输入
from openpyxl import load_workbook
wb = load_workbook('file.xlsx')
print(wb.sheetnames)
for worksheets in wb.sheetnames:
worksheet = wb[worksheets]
print(worksheet)
您需要遍历工作表的名称 (wb.sheetnames),而不是遍历工作表的索引 (wb)