簡體   English   中英

如何跳過此代碼中的錯誤? (涉及 Tkinter 和 openpyxl)

[英]how to skip an error in this code?? (Tkinter and openpyxl involved)

感謝您的幫助!

我有一個電子表格記錄我的收入和支出,包括日期、描述、借方和貸方金額。 我想使用 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]

可能是一個不錯的選擇

try:
   your code
except:
    pass

並在其中運行您的代碼,您希望通過此嘗試跳過錯誤,但除外。 如果發生任何異常,它將忽略這一點並運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM