首页 文章

Xlwings - 引用保存的范围并附加其他值

提问于
浏览
0

我是一个非常缺乏经验的程序员,并且一直在尝试在Xlwings中练习一些基础知识,这是一个与Excel一起工作的Python库 . 我在2.7工作 .

我用GUI创建了一个小程序,允许您只需在Excel中的列中添加条目 . 我在尝试我的程序后立即注意到它会覆盖以前保存的值 . 我完全迷失在找到一个可行的解决方案,如何从列表中开始,然后继续追加它 .

from Tkinter import *
from xlwings import Workbook, Range

root = Tk()
wb = Workbook()

e1 = Entry()
e1.grid(row=1, column=2)

participant_list = [] 
"""I realize starting with an empty list will clear the range every time I
run the script, but am not sure what the correct solution should be."""

def pressed_button():

    entry = e1.get()
    e1.delete(0, END) # clear the entry widget
    temp = []
    temp.append(entry)
    participant_list.append(temp)
    Range('A1').value = participant_list
    print participant_list # just to test

b1 = Button(text="Add Participant", command=pressed_button)
b1.grid(row=2, column=2)

mainloop()

任何有助于我完成解决方案的帮助将不胜感激!我尝试了一些不同的东西,但是太尴尬了,不能把它们放在我的演示代码中 .

1 回答

  • 0

    根据您的要求,您可以先读取数据并将其分配给 participant_list ,也可以找到该列中最后使用过的单元格:例如 . 如果没有空行,就像

    rng = Range('A1').vertical.last_cell
    ...
    Range((rng.row + 1, rng.column)).value = participant_list
    

    可以工作 .

相关问题