I want to import all data(headers, columns, rows) from an any sample MS Excel spreadsheet to a tkinter frame. I have tried to do it but didn't work. Can someone help me with this task ? Thank you!
This is the code I developed until now, but in this code there are buttons to import data via columns. What my expectation is to directly import all the rows, columns as it is in the excel file without any button clicks. Also the columns width should be re-sizable.
from tkinter import *
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
root = Tk()
root.title('Excel Sheet via Tkinter')
# root.iconbitmap('')
root.geometry("500x800")
# Create workbook instance
wb = Workbook()
# Load existing workbook
wb = load_workbook('test.xlsx')
# Create active worksheet
ws = wb.active
# Create variable for Column A
column_a = ws['A']
column_b = ws['B']
def get_a():
list = ''
for cell in column_a:
list = f'{list + str(cell.value)}\n'
label_a.config(text=list)
def get_b():
list = ''
for cell in column_b:
list = f'{list + str(cell.value)}\n'
label_b.config(text=list)
ba = Button(root, text="Get Column A", command=get_a)
ba.pack(pady=20)
label_a = Label(root, text="")
label_a.pack(pady=20)
bb = Button(root, text="Get Column B", command=get_b)
bb.pack(pady=20)
label_b = Label(root, text="")
label_b.pack(pady=20)
root.mainloop()
PS : I'm very new to Python so please bare with me.
Use read only mode to get all data from excel to list in Openpyxl, each sublist represent a row so you can find column number by
len()
on each row. Then back to tkinter create number of frames that match total number of rows from list. Each frames create number of note that determine by number of columns each row. And assign all cell’s info from list to tkinker’s notes