将树视图中的内容导出到 excel 文件的最短方法是什么

what is shortest way to export what's in a tree view to an excel file

我有一个 treeView 有很多数据我想将这些数据导出到 excel 文件。 我想要最简单的方法。

我有一个想法 是将树视图中的内容转换为数据框,然后将数据框另存为excel 文件。 但我不知道这是否是个好主意?!

from tkinter import filedialog
import pandas as pd
from collections import defaultdict

file = filedialog.asksaveasfilename(title="Select file","nameOfFile.xlsx",filetypes=[("Excel file", "*.xlsx")])
if file:
    ids=tree.get_children()
    dict = defaultdict(list)
    for id in ids:
        date=dt.datetime.strptime(tree.set(id, "#13"), '%Y-%m-%d %H:%M:%S')
        dateChosed=dt.datetime.strptime(monthToExport.get(), "%B-%Y")
        if (date.year == dateChosed.year) and (date.month == dateChosed.month):
            dict["1"].append(tree.item(id)["text"])
            dict["2"].append(tree.item(id)["values"][0])

    dict = pd.DataFrame.from_dict(dict)
    try:
        dict.to_excel(file, engine='xlsxwriter',index= False)
    except:
        print("Close the file than retry")
else:
    print("You did not save the file")
from tkinter import filedialog
import pandas as pd
from collections import defaultdict

file = filedialog.asksaveasfilename(title="Select file","nameOfFile.xlsx",filetypes[("Excel file", "*.xlsx")])
if file:
    ids=tree.get_children()
    dict = defaultdict(list)
    for id in ids:
        date=dt.datetime.strptime(tree.set(id, "#13"), '%Y-%m-%d %H:%M:%S')
        dateChosed=dt.datetime.strptime(monthToExport.get(), "%B-%Y")
        if (date.year == dateChosed.year) and (date.month == dateChosed.month):
            dict["1"].append(tree.item(id)["text"])
            dict["2"].append(tree.item(id)["values"][0])

    dict = pd.DataFrame.from_dict(dict)
    try:
       dict.to_excel(file, engine='xlsxwriter',index= False)
    except:
       print("Close the file than retry")
else:
print("You did not save the file")