如何在Tkinter中将Treeview数据另存为Excel文件? [英] How to save a Treeview data as excel file in Tkinter?

查看:56
本文介绍了如何在Tkinter中将Treeview数据另存为Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Treeview 表单中有一些数据已绑定到我的SQLite数据库,我想知道是否有一种方法可以将 Treeview 的数据另存为Excel"文件在我的计算机上.

I have some data in my Treeview Form which is binded to my SQLite database, I wonder if there is a way to save my Treeview's data as an excel file in my computer.

mytree = ttk.Treeview(frame_busqueda, height=19, column=('column1', 'column2', 'column3', 'column4', 'column5', 'column6'), show='headings', style="Custom.Treeview")
            mytree.pack()
style = ttk.Style()

style.configure("Custom.Treeview.Heading",
                             foreground="green", relief="flat", font='arial 10 bold')
style.map("Custom.Treeview.Heading", relief=[('active', 'groove'), ('pressed', 'sunken')])


mytree.heading("#1", text="ID CARD")
mytree.column("#1", minwidth=0, width=103, stretch=NO)
mytree.heading("#2", text="NAME")
mytree.column("#2", minwidth=0, width=200, stretch=NO)
mytree.heading("#3", text="SURNAME") 

def savetreeview():
     with open("new.csv", "w", newline='') as myfile:
          csvwriter = csv.writer(myfile, delimiter=',')

          for row_id in mytree.get_children():
              row = mytree.item(row_id)['values']
              csvwriter.writerow(row)
              print(row)

我试图使用csv尝试获取行,它向我显示了所有 Treeview 数据作为输出,但是实际上我想要保存的是 Treeview 的数据作为我计算机中的excel文件.谢谢

I was trying to use csv to try to get the rows, and it shows me all my Treeview data as output, but actually what I want is to save my Treeview's data as an excel file in my computer. Thanks

推荐答案

使用 pandas 非常简单,您要做的就是创建一个writer,并使用它将csv写入Excel,像:

This is pretty simple with pandas, all you have to do is create a writer and use it to write the csv to excel, like:

import pandas as pd

writer = pd.ExcelWriter('nameofexcelfiletocreate.xlsx') # Creates this excel file
df = pd.read_csv('new.csv') # Reads the csv and makes a dataframe

df.to_excel(writer,'sheet1') # Writes the dataframe to excel file
writer.save() # Saves the file

这将创建一个新的excel文件,并将您的csv转换为excel.请注意,您必须安装 openpyxl 才能运行:

This will create a new excel file with your csv converted to excel. Keep a note you have to install openpyxl for this to function:

pip install openpyxl

所以您的功能将类似于:

So your function would be something like:

def call():
    cols = ['ID CARD','NAME','SURNAME'] # Your column headings here
    path = 'read.csv'
    excel_name = 'newfile.xlsx'
    lst = []
    with open(path, "w", newline='') as myfile:
        csvwriter = csv.writer(myfile, delimiter=',')
        for row_id in mytree.get_children():
            row = mytree.item(row_id,'values')
            lst.append(row)
        lst = list(map(list,lst))
        lst.insert(0,cols)
        for row in lst:
            csvwriter.writerow(row)

    writer = pd.ExcelWriter(excel_name)
    df = pd.read_csv(path)
    df.to_excel(writer,'sheetname')
    writer.save()

这篇关于如何在Tkinter中将Treeview数据另存为Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆