Openpyxl到treeview传递信息

时间:2017-04-20 12:42:04

标签: python-2.7 treeview openpyxl

我试图将一些数据从excel文件传递到python中的树视图。我无法知道如何将每个单元格值分配到不同的列。我有一个有三列的树视图。

从excel文件中获取数据并传递给树视图:

import Tkinter as tk
import ttk
from openpyxl import load_workbook

self.add_packing_tree = ttk.Treeview(self.left_area_frame_top_bottom, selectmode='extended')
self.add_packing_tree['columns'] = ('ID', 'Nro Parte', 'Cantidad')
self.add_packing_tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.add_packing_tree.column('#0', anchor=tk.W, minwidth=0, stretch=0, width=0)
self.add_packing_tree.column('ID', anchor=tk.W, minwidth=50, stretch=1, width=50)
self.add_packing_tree.column('Nro Parte', anchor=tk.W, minwidth=50, stretch=1, width=50)
self.add_packing_tree.column('Cantidad', minwidth=50, stretch=1, width=50)
self.add_packing_tree.heading('ID', text="ID")
self.add_packing_tree.heading('Nro Parte', text="Nro Parte")
self.add_packing_tree.heading('Cantidad', text="Cantidad")

file_location = self.filename
workbook = xl.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
self.file_route.set(self.filename)
wb = load_workbook(self.filename)
sheet = wb.active
for row in sheet.iter_rows(min_row=2, min_col=0):
   for cell in row:
      self.add_packing_tree.insert('', 'end', values=(cell.value))

它的效果很好,但在树视图中,所有内容都在第一列。

感谢。

1 个答案:

答案 0 :(得分:0)

  

** Python»3.6.1文档»**:tkinter.ttk.html#item-options
  值
  与项目关联的值列表。

更改为以下内容:

for row in sheet.iter_rows(min_row=2, min_col=0):
  self.add_packing_tree.insert('', 'end', values=[cell.value for cell in row])