how can i read excel cells through openpyxl using pysimplegui in python?

Alright, I figured it out. The code in itself is fine. The reason why it doesn’t read the cells it’s supposed to read is that PySimpleGUI is needs to know those Excel cells are integers. So the only thing I did was to add int(values[‘ID’])

I made another mod in the code. Since I’m already importing the excel file into the code using openpyxl.load_workbook, there is no need to browse for it in the window. I did away with that part. Here is the complete functional code:

import PySimpleGUI as sg
import openpyxl

roster = openpyxl.load_workbook('/home/superuser/Desktop/sample.xlsx')
sheet = roster.active  

sg.theme('DefaultNoMoreNagging')
#sg.theme('TanBlue')
#sg.theme('Material2')
layout =  [[sg.Text(pad=(150,30), font=("Arial", 20, 'bold'), size=(15,1))],
          [sg.Text("Student ID:", font=("Arial", 15, 'bold'), pad=(20,0), size=(12,1)), sg.InputText(key='ID', font="Arial 20", background_color="#F7F9F9", size=(10,1)),sg.Button("Enter",font="Arial 10", pad=(10,0))],
          [sg.Text("Student Name:", font=("Arial", 15, 'bold'), pad=(20,30), size=(12,1)), sg.Multiline(key='name', font="Arial 20", pad=(0,5), background_color="#F7F9F9", size=(17,1), do_not_clear=False)]]

window = sg.Window("Working Code", layout)

while True:
    event,values = window.read()
    if event == "Cancel" or event == sg.WIN_CLOSED:
        break
    elif event == "Enter":        
        for row in sheet.rows:
            if  int(values['ID']) == row[2].value:
                print("Student Name:{} {}".format(row[0].value,row[1].value))        
                window['name'].update(f"{row[0].value} {row[1].value}")
                break
        else:
            sg.popup_error("Record not found", font="Arial 10")

window.close()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top