This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hello,
I am learning about python, Selenium for automation and tkinter gui
My application reads data from an excel document and submit the informaiton to a website.
These is the source code for the part that reads from excel:
import openpyxl
from openpyxl import load_workbook
class Excel_DataParse:
def __init__(self):
self.data_to_add=[]
def abrir_workbook(self):
self.wb=load_workbook(filename=self.file_path,data_only=True)
def procesar_fila(self,num_fila):
print("Procesando linea: " str(num_fila))
#valores por defecto
case_consult=False
case_escal=False
Week_Rev=False
case_reop=False
SLA_meet=True
case_consult=False
case_escal=False
case_reop=False
case_comments="No"
case_es_cons_date=""
case_reop_date=""
ws=self.ws
print(type(ws))
source=ws['D' str(num_fila)].value
status=ws['M' str(num_fila)].value
creation_date=ws['K' str(num_fila)].value.date().strftime('%m/%d/%Y')
if ws['N' str(num_fila)].value == 'N':
SLA_meet=False;
case_no=ws['C' str(num_fila)].value
if source=="IPS cloud": #this probably should be moved to the excel_source.py file
case_no=str(case_no)
while (len(case_no)<8):
case_no='0' case_no
case_geo=ws['I' str(num_fila)].value
case_cat=ws['E' str(num_fila)].value
case_pr=ws['G' str(num_fila)].value
case_gr=ws['H' str(num_fila)].value
case_topic=ws['F' str(num_fila)].value
working_date=ws['B' str(num_fila)].value.date().strftime('%m/%d/%Y')
case_min=ws['J' str(num_fila)].value
case_min=case_min*60
case_hour,case_min=divmod(case_min,60)
case_min=str(int(case_min))
case_hour=str(int(case_hour))
if status=='consultation':
case_consult=True;
case_es_cons_date=working_date
elif status=='escalated':
case_escal=True;
case_es_cons_date=working_date
if ws['O' str(num_fila)].value == 'Y':
Week_Rev=True;
if ws['P' str(num_fila)].value == 'Y':
case_reop=True
case_reop_date=working_date
if ws['Q' str(num_fila)].value!=None:
case_comments=ws['Q' str(num_fila)].value
t=[source, status, creation_date, SLA_meet, case_no, case_geo, case_cat, case_pr, case_gr,case_topic,\
working_date, case_hour, case_min, Week_Rev,case_consult, case_escal,case_es_cons_date,case_reop, case_reop_date,\
case_comments]
self.data_to_add.append(t)
def mostrar_item(self,num_fila):
i=num_fila-self.p_linea
t=self.data_to_add[i]
print("Source: " t[0])
print("Status: " t[1])
print("Creation Date: " str(t[2]))
if t[3]:
print("SLA_meet? Yes")
else:
print("SLA_meet? No")
print("Case Number: " str(t[4]))
print("GEO: " t[5])
print("Category: " t[6])
print("Product: " t[7])
print("Group: " t[8])
print("Title: " t[9])
print("Working Date: " str(t[10]))
print("Case hours: " str(t[11]))
print("Case minutes: " str(t[12]))
if t[13]:
print("Reviewed on Weekly Meeting? Yes")
else:
print("Reviewed on Weekly Meeting? No")
if t[14]:
print("Case Consulted? Yes")
print("Consulted date: " str(t[16]))
else:
print("Case Consulted? No")
if t[15]:
print("Case Escalated? Yes")
print("Consulted date: " str(t[16]))
else:
print("Case Escalated? No")
if t[17]:
print("Case Reopen? Yes")
print("Reopen date: " str(t[18]))
else:
print("Case Reopen? No")
print("Comments: " t[19])
def lee_datos(self,file_path,p_linea,u_linea):
self.p_linea=p_linea
self.file_path=file_path
if file_path!="None":
self.abrir_workbook()
self.ws=self.wb["Time Report"]
for i in range(p_linea,u_linea 1):
self.procesar_fila(i)
else:
print("No valid file found")
This is the GUI:
from tkinter import Tk, Label, Button,Entry
from tkinter import filedialog
from tkinter import Toplevel
from tkinter import StringVar, IntVar
import getpass
from excel_source import Excel_DataParse
class DB_GUI:
def __init__(self,master,excel):
self.excel=excel
self.sykes_uname=""
self.sykes_pass=""
self.p_line=0
self.u_line=0
self.master=master
self.filename=""
master.title("A Simple GUI")
self.label=Label(master,text="Insertar casos en la base de datos")
self.label.pack()
self.log_button=Button(master,text="Ingresar login de Sykes", command=lambda:self.login_det_wind(master))
self.log_button.pack()
self.excel_button=Button(master,text="Buscar archivo fuente", command=lambda:self.excel_window(master))
self.excel_button.pack()
self.load_button=Button(master,text="Cargar datos de excel", command=lambda:Excel_DataParse.lee_datos(self.excel,self.filename,self.p_line,self.u_line))
self.load_button.pack()
self.test_button=Button(master,text="Probar", command=lambda:Excel_DataParse.mostrar_item(1084))
self.test_button.pack()
self.close_button=Button(master, text="Cerrar", command=master.quit)
self.close_button.pack()
def close_top(self,window):
window.destroy()
window.update()
def update_entry_val(self,entry1,entry2,window):
if((type(entry1) is StringVar) and (type(entry2) is StringVar)):
self.sykes_uname=entry1.get()
self.sykes_pass=entry2.get()
elif((type(entry1) is IntVar) and (type(entry2) is IntVar)):
self.p_line=entry1.get()
self.u_line=entry2.get()
self.close_top(window)
def excel_window(self,master):
self.prim_linea=IntVar()
self.ult_linea=IntVar()
exl_wind=Toplevel(master)
self.main_label=Label(exl_wind,text="Informacion sobre el archivo fuente")
self.main_label.grid(row=0, column=1)
self.excel_button=Button(exl_wind,text="Buscar archivo excel", command=self.get_source_file)
self.excel_button.grid(row=1, column=1)
self.prim_l_label=Label(exl_wind,text="Primera Linea: ")
self.prim_l_label.grid(row=2, column=0)
self.plinea_entry=Entry(exl_wind,textvariable=self.prim_linea)
self.plinea_entry.grid(row=2,column=1)
self.prim_l_label=Label(exl_wind,text="Ultima Linea: ")
self.prim_l_label.grid(row=3, column=0)
self.ulinea_entry=Entry(exl_wind,textvariable=self.ult_linea)
self.ulinea_entry.grid(row=3,column=1)
self.ok_button=Button(exl_wind,text="Acceptar",command=lambda:self.update_entry_val(self.plinea_entry,self.ulinea_entry,exl_wind))
self.ok_button.grid(row=4, column=2)
self.cancel_button=Button(exl_wind,text="Cancelar",command=lambda:self.close_top(exl_wind))
self.cancel_button.grid(row=4, column=0)
def login_det_wind(self,master):
self.sks_login=StringVar()
self.sks_pass=StringVar()
log_wind=Toplevel(master)
log_wind.title("Sykes Login")
self.main_label=Label(log_wind,text="Ingrese su nombre de usuario y contrasena")
self.main_label.grid(row=0, column=1)
self.uname_label=Label(log_wind,text="Usuario de Sykes:")
self.uname_label.grid(row=1,column=0)
self.pass_label=Label(log_wind,text="Contraseña:")
self.pass_label.grid(row=2,column=0)
self.uname_entry=Entry(log_wind,textvariable=self.sks_login,show='*')
self.uname_entry.grid(row=1,column=1)
self.pass_entry=Entry(log_wind,textvariable=self.sks_pass)
self.pass_entry.grid(row=2,column=1)
self.ok_button=Button(log_wind,text="Acceptar",command=lambda:self.update_entry_val(self.uname_entry,self.pass_entry,log_wind))
self.ok_button.grid(row=3, column=2)
self.cancel_button=Button(log_wind,text="Cancelar",command=lambda:self.close_top(log_wind))
self.cancel_button.grid(row=3, column=0)
def get_source_file(self):
os_user=getpass.getuser()
doc_path="C:\\Users\\" os_user "\\Documents"
self.filename=filedialog.askopenfilename(initialdir=doc_path,title="Select file",filetypes=(("excel files","*.xlsx"),("csv files","*.csv"),("all files","*.*")))
if not self.filename:
self.filename="None"
root=Tk()
excel_cl=Excel_DataParse()
my_gui=DB_GUI(root,excel_cl)
root.mainloop()
When I call the method lee_datos from a direct instance of Excel_DataParse class, works fine.
However when I call lee_datos from the GUI by pressin load_button it returns an error:
'tuple' object has no attribute 'value'
When reading this line :
source=ws['D' str(num_fila)].value
from method procesar_fila
Also I would like to point out that this is my first attempt at:
1) Separating python code on different classes
2) Separating python code on differente files
3) Calling a method from one class inside other class
So any particular feedback about my code and how to improve it, would be appreciated, I am specially in doubt about when to use self paramaters.
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...