如何跳过此代码中的错误? (涉及Tkinter和openpyxl)

时间:2020-07-07 16:16:38

标签: python tkinter openpyxl

感谢您的帮助!

我有一个电子表格,其中记录了我的收入和支出,以及日期,描述,借方和贷方金额。我想使用openpyxl来自动化添加特定类别中的值的过程,该程序现在可以正常工作,但是问题是:当我搜索工作表中不存在的短语时,程序崩溃,它无法完成不存在的搜索词组之前和之后的假定操作。

例如, 当我想计算搜索短语“工资”的小计并将小计放入目标单元格时,它工作正常。当我要求程序寻找在那里不存在的东西时,问题就会出现。

我要求程序寻找工资,创建小计,可以正常工作,并假设将值存储在已定义的目标上,当我要求程序寻找税(不存在)时,程序什么都没显示,然后我要求程序获取租金小计(已存在)。该程序无法进行更改。

我在这方面还比较陌生。...因此再次感谢您的帮助! :)

from tkinter import *
import openpyxl as xl

window = Tk()
window.title("Excel Automation")  # rename the title


def define_subtotal():
    file_name = str(file_name_input_field.get())  # Collects the text from the text entry box
    sheet_name = str(sheet_name_label_name_input_field.get())  # Collects the text from the text entry box
    global wb   # declare Workbook as global variable
    wb = xl.load_workbook(file_name)  # define workbook
    sheet = wb[sheet_name]  # Define sheet name
    col_num = int(search_column_input_field.get())
    search_phrase = search_phrase_input_field.get()
    offset_col = int(offset_col_input_field.get())
    target_col_num = int(target_col_input_field.get())
    target_row_num = int(target_row_input_field.get())

    total = 0
    for row in range(2, sheet.max_row + 1):
        cell = sheet.cell(row, col_num)
        if cell.value is None:
            continue
        else:
            if search_phrase.casefold() in str(cell.value).casefold():
                total += cell.offset(column=offset_col).value
                total_description = sheet.cell(target_row_num, target_col_num + 1)
                total_description.value = "Subtotal of : " + search_phrase
                total_cell = sheet.cell(target_row_num, target_col_num)
                total_cell.value = total
                output.delete(0.0, END)
                output.insert(END, "Subtotal for " + search_phrase + " defined")
            else:
                continue


def execute():
    output.delete(0.0, END)  # clear the text box
    new_file_name = new_file_input_field.get()
    output.insert(END, "Calculations complete!")
    wb.save(new_file_name + ".xlsx")


def import_excel_file():
    output.delete(0.0, END)  # clear the text box
    output.insert(END, "File imported")
    sheet_name_label_name_input_field.config (state='disabled')
    import_button.config (state='disabled')
    file_name_input_field.config (state='disabled')


def close_window():  # exit function
    window.destroy()
    exit()


### CONTENTS

file_name_label = Label(window, text="File Name:")
file_name_input_field = Entry(window, width=38, borderwidth=2)
sheet_name_label = Label(window, text="Sheet Name:")
sheet_name_label_name_input_field = Entry(window, width=38, borderwidth=2)
import_button = Button(window, text="Import", padx=35, pady=0, command=import_excel_file)
search_phrase_label = Label(window, text="Search Phrase:")
search_phrase_input_field = Entry(window, width=38, borderwidth=2)
search_column_label = Label(window, text="Search Column:")
search_column_input_field = Entry(window, width=38, borderwidth=2)
offset_col_label = Label(window, text="Offset Column:")
offset_col_input_field = Entry(window, width=38, borderwidth=2)
target_col_label = Label(window, text="Target Column:")
target_col_input_field = Entry(window, width=38, borderwidth=2)
target_row_label = Label(window, text="Target Row:")
target_row_input_field = Entry(window, width=38, borderwidth=2)
new_file_label = Label(window, text="Name of New file:")
new_file_input_field = Entry(window, width=38, borderwidth=2)
define_subtotal_button = Button(window, text="Define Subtotal", padx=5, pady=0, command=define_subtotal)
execute_button = Button(window, text="Execute", padx=5, pady=0, command=execute)
# contents Column 2
status_label = Label(window, text="Status:")
output = Text(window, width=50, height=25, wrap=WORD, bg="white")  # wrap=WORD : wrap text when in overflow.
output.insert(END, "Drag and drop file into project file\n"
                   "Define File Name and Sheet Name\n"
                   "Example: filename.xlsx /.xlsm/ xltx/.xltm")
exit_button = Button(window, text="exit", width=14, command=close_window)

### THE GRID

file_name_label.grid(row=0, column=0, columnspan=2, padx=0, pady=0, sticky=W)
file_name_input_field.grid(row=1, column=0, columnspan=2, padx=5, pady=3, sticky=W)
sheet_name_label.grid(row=2, column=0, columnspan=2, padx=0, pady=0, sticky=W)
sheet_name_label_name_input_field.grid(row=3, column=0, columnspan=2, padx=5, pady=3, sticky=W)
import_button.grid(row=4, column=0, columnspan=2, sticky=W, padx=5)
search_phrase_label.grid(row=5, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_phrase_input_field.grid(row=6, column=0, columnspan=2, padx=5, pady=5, sticky=W)
search_column_label.grid(row=7, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_column_input_field.grid(row=8, column=0, columnspan=2, padx=5, pady=5, sticky=W)
offset_col_label.grid(row=9, column=0, columnspan=2, padx=0, pady=0, sticky=W)
offset_col_input_field.grid(row=10, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_col_label.grid(row=11, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_col_input_field.grid(row=12, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_row_label.grid(row=13, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_row_input_field.grid(row=14, column=0, columnspan=2, padx=5, pady=5, sticky=W)
new_file_label.grid(row=15, column=0, columnspan=2, padx=0, pady=0, sticky=W)
new_file_input_field.grid(row=16, column=0, columnspan=2, padx=5, pady=5, sticky=W)
define_subtotal_button.grid(row=17, column=0, sticky=W, padx=5)
# GRID column 1
execute_button.grid(row=17, column=1, sticky=W, padx=5)
# GRID Column 2
status_label.grid(row=0, column=2, padx=5, sticky=W)
output.grid(row=1, column=2, rowspan=25, padx=5, sticky=NE)
exit_button.grid(row=17, column=2, sticky=E)

window.mainloop()

[enter image description here][1]

1 个答案:

答案 0 :(得分:0)

也许是一个很好的选择

try:
   your code
except:
    pass

并在其中运行您的代码,并尝试通过该代码跳过错误。如果发生任何异常,它将忽略此消息并将其运行。

相关问题