У меня есть файл .xlsx
, и я хочу воссоздать эту таблицу в GUI, используя Treeview от TKinter. У меня есть решение ниже, которое дает мне вывод, который я хочу, но он длинный, и я не уверен, есть ли лучший способ сделать это. Для моего приложения важна производительность, потому что у меня не так много энергии, и я думаю, что с большим файлом .xlsx
я начну видеть снижение производительности.
Я также должен предположить, что я Не знаю заголовок и количество строк, но количество столбцов меньше 15
import numpy as np
import pandas as pd
xls = pd.ExcelFile('file.xlsx');
sheetData = pd.read_excel(xls, 'Sheet-1')
# Get column headings
headings = sheetData.columns
# Convert headings to list
data = list(headings.values.tolist())
# Get row count
rows = len(sheetData)
# Create tree with the the number of columns
# equal to the sheet, the id of the column
# equal to the column header and disable
# the 'treeview'
tree = ttk.Treeview(self, columns=data, show=["headings"],selectmode='browse')
# Create column headings on tree
for heading in headings:
heading = str(heading) # convert to string for processing
tree.column(heading, width=125, anchor='center')
tree.heading(heading, text=heading)
# Populate rows --The part that concerns me
for rownumber in range(rows):
rowvalue = sheetData.values[rownumber] # Get row data
rowvalue = np.array2string(rowvalue) # Convert from an np array to string
rowvalue = rowvalue.strip("[]") # Strip the string of square brackets
rowvalue = rowvalue.replace("'",'') # Replace all instances of ' with no character
tree.insert('', 'end', values= rowvalue) # Append the row to table
Есть ли более простой способ получить данные строки и добавить их в древовидную структуру?