Clay Finney February 2016

Xlwings - Referencing saved range and appending additional values

I am a pretty inexperienced programmer and have been trying to practice some basics in Xlwings, a Python library that works with Excel. I am working in 2.7.

I have made a small program with GUI that allows you to simply add entries down a column in Excel. I noticed immediately upon trying my program that it overwrites the previously saved values down the column. I am completely lost in figuring out a workable solution on how to start with a list featuring the range, and then continuing to append to it.

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()

Any help in walking me through a solution would be greatly appreciated! I've tried a few different things, but am too embarrassed to put them in my demo code ha.

Answers


Felix Zumstein February 2016

Depending on what you want to do, you could either read in your data first and assign it to participant_list or you could find out the last used cell in that column: E.g. if there are no empty rows, something like

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

could work.

Post Status

Asked in February 2016
Viewed 3,307 times
Voted 13
Answered 1 times

Search




Leave an answer