zhudaming120 发表于 2020-5-8 21:47:00

关于向exce表格中插入列表的问题

import tkinter as tk
from tkinter import *
from openpyxl import load_workbook
import datetime as tm
import os
import xlrd
import xlwt
win = tk.Tk()
win.title("管理系统")
win.geometry("800x700")
var1 = tk.StringVar()
l = tk.Label(win,bg="yellow",width=4,textvariable=var1)
l.pack()
def pringt_selection():
    value1 = lb.get(lb.curselection())
    value = value1[:-5]


    var1.set(value)
    print(value)
    workbook = load_workbook("D:\\例子\\%s.xlsx" % (value))
    sheet = workbook.active
    cell = sheet["A1"]
    cell.value = "haha"
    workbook.save("D:\\例子\\%s.xlsx" % (value))

b1 = tk.Button(win,text="111",width=15,height=2,command=pringt_selection)
b1.pack()

var2=tk.StringVar()
#var2.set((11,22,33,44))

lb=tk.Listbox(win,listvariable=var2)
for i in os.listdir("D:\\例子\\"):
    lb.insert("end",i)
lb.pack()

win.mainloop()

按照这样的代码实现了向特定excell表格中插入内容



import tkinter as tk
from tkinter import *
from openpyxl import load_workbook
import datetime as tm
import os
import xlrd
import xlwt
win = tk.Tk()
win.title("管理系统")
win.geometry("800x700")
var1 = tk.StringVar()
l = tk.Label(win,bg="yellow",width=4,textvariable=var1)
l.pack()
def pringt_selection():
    value1 = lb.get(lb.curselection())
    value = value1[:-5]


    var1.set(value)
    print(value)
    workbook = load_workbook("D:\\例子\\%s.xlsx" % (value))
    sheet = workbook.active
    data =["时间","地点","人物"]
    for i in data:
      sheet.append(i)
    workbook.save("D:\\例子\\%s.xlsx" % (value))

b1 = tk.Button(win,text="111",width=15,height=2,command=pringt_selection)
b1.pack()

var2=tk.StringVar()
#var2.set((11,22,33,44))

lb=tk.Listbox(win,listvariable=var2)
for i in os.listdir("D:\\例子\\"):
    lb.insert("end",i)
lb.pack()

win.mainloop()



但是将写入的内容换成列表后就会报错,
workbook = load_workbook("D:\\例子\\%s.xlsx" % (value))
    sheet = workbook.active
    data =["时间","地点","人物"]
    for i in data:
      sheet.append(i)
    workbook.save("D:\\例子\\%s.xlsx" % (value))
插入列表的错误在哪里,求正确的写法{:5_111:}

rsj0315 发表于 2020-5-8 23:29:53

不需要用for循环来添加列表的每一项。
直接sheet.append(data)
就可以了

zhudaming120 发表于 2020-5-9 12:40:34

谢了 ,原来画蛇添足了

zhudaming120 发表于 2020-5-9 12:41:22

rsj0315 发表于 2020-5-8 23:29
不需要用for循环来添加列表的每一项。
直接sheet.append(data)
就可以了

怎么不能给你最佳答案啊?

rsj0315 发表于 2020-5-10 21:01:23

zhudaming120 发表于 2020-5-9 12:41
怎么不能给你最佳答案啊?

不清楚
页: [1]
查看完整版本: 关于向exce表格中插入列表的问题