如何更快地将Excel和熊猫的值导入tkinter?

时间:2020-01-02 15:07:16

标签: python excel pandas tkinter openpyxl

伙计们!你好吗?我下面有这段代码,而我在insertData函数中遇到了麻烦。此功能用于使用Pandas将excel电子表格中的值输入到tkinter树视图中。几乎可以,但是它太慢了,我不知道如何解决。如果有人可以帮助我,我会很高兴。 预先感谢!

from tkinter import *
import ttk
import openpyxl
import pandas as pd

nuScreen = Tk()
nuScreen.title("ContrasinSystem - Usuário Comum")
nuScreen.iconbitmap("logocontransin.ico")

book = openpyxl.load_workbook('Registros.xlsx')
sheet = book.sheetnames
sh = book.active.cell

#Contador de Linhas:
wb2 = openpyxl.load_workbook('Registros.xlsx')
sheet2 = wb2.worksheets[0]

rowCount = sheet2.max_row

v = []

#Design:


class main():
    def __init__(self,tk):
        for x in range (0,len(sheet)):
            v.append(sheet[int(x)])


        self.wb2 = openpyxl.load_workbook('Registros.xlsx')
        self.sheet2 = self.wb2.worksheets[0]

        self.row_count = self.sheet2.max_row
        self.column_count = self.sheet2.max_column


        self.nuFrame = Frame(nuScreen, width = 1500, height = 450)

        self.nuFrame.pack()

        self.img = PhotoImage(file="logocontransin.png")
        self.w = Label(self.nuFrame, image = self.img)
        self.w.img = self.img
        self.w.place(x=65,y=150)


        self.srchlbl = ttk.Label(self.nuFrame, text = "Buscar:")
        self.srchlbl.place(x=25,y=75)
        self.srchetr = ttk.Entry(self.nuFrame, width = 30)
        self.srchetr.place(x=75,y=75)

        self.treeview = ttk.Treeview(self.nuFrame)
        self.treeview.place(x=300,y=75, width = 1100)

        dataVector = []      
        def columnsName():
            def Header():
                self.columnVector = []
                self.dataVector = []
                teste = []
                self.treeview.column("#0", width = 20)          
                self.columnHeader = pd.read_excel(r'Registros.xlsx', str(self.cmb.get()), header_only = True, nrows=0).columns
                for a in self.columnHeader:
                    self.columnVector.append(a)
                self.treeview.configure(columns = self.columnVector)
                for b in self.columnHeader:
                    self.treeview.heading(str(b), text = str(b))
            def insertData():
                for m in range(rowCount):
                    self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), skip_blank_lines=True, skiprows=0)
                for l in self.dataValues:
                    dataVector.append(l)
                self.treeview.insert("", "end",values = dataVector)
                print(self.dataValues)

            Header()
            insertData()


        self.cmbLbl = ttk.Label(self.nuFrame, text = "Assunto:")
        self.cmbLbl.place(x=1200, y=325)
        self.cmb = ttk.Combobox(self.nuFrame, values = v)
        self.cmb.place(x=1250,y=325)

        self.btncmb = ttk.Button(self.nuFrame, text = "Buscar", command = columnsName)
        self.btncmb.place(x=1320,y=375)

nuScreen.geometry("1500x450")
main(nuScreen)
nuScreen.mainloop()

1 个答案:

答案 0 :(得分:0)

您想打开一个excel文件进​​行读取几次?

def insertData():
  for m in range(rowCount):
    self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), 
    skip_blank_lines=True, skiprows=0)
  for l in self.dataValues:
    dataVector.append(l)
    self.treeview.insert("", "end",values = dataVector)
    print(self.dataValues)

您是否应该忽略第一个循环?

def insertData():
  self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), 
  skip_blank_lines=True, skiprows=0)
  for l in self.dataValues:
    self.treeview.insert("", "end",values = l)
    print(self.dataValues)