I'm trying to create an application that will ask the user to input details of new proposals received. And then add the entries to an excel sheet. My code has prepared the excel file and I added all required labels and entry fields, I'm having trouble in transferring the contents of the entry fields to the excel file.
What I believe my main problem to be is in the new_entry
function. I'm not getting any errors, the code is just not working as intended.
My code is as follows
from openpyxl import *
import tkinter as tk
# 1 is for client name
# 2 is for service type
# 3 is for Deadline date
# 4 is for responsible party
def new_entry():
new_line = sheet.max_row + 1
sheet.cell(column=1, row=new_line, value=txtfld_1.get())
sheet.cell(column=2, row=new_line, value=txtfld_2.get())
sheet.cell(column=3, row=new_line, value=txtfld_3.get())
sheet.cell(column=4, row=new_line, value=txtfld_4.get())
def close_window():
window.destroy()
try:
workbook = load_workbook(filename="Received Proposals.xlsx")
sheet = workbook.active
except:
workbook = Workbook()
sheet = workbook.active
sheet["A1"] = "Client Name"
sheet["B1"] = "Required Service"
sheet["C1"] = "Client Deadline"
sheet["D1"] = "Responsible Consultant"
window = tk.Tk()
window.title('New Proposal Window')
window.geometry("500x200")
btn_exit = tk.Button(window, text="Exit", fg='black', command = close_window)
btn_exit.place(x=400, y=160)
lbl_0 = tk.Label(window, text="New Proposal Entry", fg='black', font=("Helvetica", 8))
lbl_0.place(x=200, y=15)
lbl_1 = tk.Label(window, text="Client Name", fg='black', font=("Helvetica", 8))
lbl_1.place(x=20, y=50)
lbl_2 = tk.Label(window, text="Required Service", fg='black', font=("Helvetica", 8))
lbl_2.place(x=20, y=75)
lbl_3 = tk.Label(window, text="Client Deadline", fg='black', font=("Helvetica", 8))
lbl_3.place(x=20, y=100)
lbl_4 = tk.Label(window, text="Responsible Consultant", fg='black', font=("Helvetica", 8))
lbl_4.place(x=20, y=125)
txtfld_1 = tk.Entry(window, bg='white',fg='black', bd=5)
txtfld_1.place(x=350, y=50)
txtfld_2 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_2.place(x=350, y=75)
txtfld_3 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_3.place(x=350, y=100)
txtfld_4 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_4.place(x=350, y=125)
btn = tk.Button(window, text="Save Entry", fg='black', command = new_entry)
btn.place(x=300, y=160)
workbook.save(filename="Received Proposals.xlsx")
window.mainloop()
You are not saving your workbook after adding the entry values. Do this -
def new_entry():
new_line = sheet.max_row + 1
sheet.cell(column=1, row=new_line, value=txtfld_1.get())
sheet.cell(column=2, row=new_line, value=txtfld_2.get())
sheet.cell(column=3, row=new_line, value=txtfld_3.get())
sheet.cell(column=4, row=new_line, value=txtfld_4.get())
workbook.save(filename="Received Proposals.xlsx")