Search code examples
pythonexcelpandastkintertreeview

Problem importing data from Excel to Treeview


I created a treeview to display the data imported from an excel file.

    def afficher():
        fichier = r"*.xlsx"
        df = pd.read_excel(fichier)
        for row in df:
            refOF = row['refOF']
            refP = row['refP']
            refC = row['refC']
            nbreP = row['nbreP']
            dateFF = row['dateFF']
            self.ordreF.insert("", 0, values=(refOF, refP, refC, nbreP, dateFF))

but I encounter the following error:

refOF = row['refOF']
TypeError: string indices must be integers

please tell me how I can solve this problem.


Solution

  • Another way is replacing the original for loop with the following:

    for tup in df[['refOF', 'refP', 'refC', 'nbreP', 'dateFF']].itertuples(index=False, name=None):
        self.ordreF.insert("", 0, values=tup)
    

    It works because df.itertuples(index=False, name=None) returns a regular tuple without index in the assigned column order. The tuple can be fed into the values= argument directly.