需要使用xlwt将数据从多个类实例保存到同一个excel文件中的不同列

时间:2013-05-07 23:40:21

标签: python excel xlwt

我正在制作这个节目。这个想法是节省从秒表到excel表的时间。但最好将它们全部保存到一个excel表中。现在它保存到多个。我想不出一种方法可以将所有秒表保存到不同列中的同一张表格中。所以,如果有人能想出办法,我会非常感激。我知道我可能会遗漏一些简单的东西,但我不得不问。我知道它没有完全评论。评论实际上只是简短的提醒我代码的作用。如果我错过了一条信息,你需要这可能有助于回答我的问题,请问。这是我的代码:

import pygtk, xlwt, pango, os
pygtk.require('2.0')
import gtk

import gobject
gobject.threads_init()

#inittial window
class mainTimer:
def new_timer(self, widget):
    new_Timer = Timers()


def destroy(self, widget, data=None):
    gtk.main_quit()

def __init__(self):
    self.timer_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.timer_window.set_position(gtk.WIN_POS_CENTER)
    self.timer_window.set_size_request(500,300)
    self.timer_window.set_title("Project Starter")
    self.button1 = gtk.Button("Pause")
    self.button1.connect("clicked", self.destroy)
    self.button1.set_tooltip_text("This will pause timer")

    self.button2 = gtk.Button("New Project Timer")
    self.button2.connect("clicked", self.new_timer)
    self.button2.set_tooltip_text("This will start a new project timer")

    fixed = gtk.Fixed()
    fixed.put(self.button1, 30, 250)
    fixed.put(self.button2, 90, 250)
    self.timer_window.add(fixed)
    self.timer_window.show_all()
    self.timer_window.connect("destroy", self.destroy)

def main(self):
    gtk.main()


class Timers:
def destroy(self, widget, data=None):
    self.gtk.main_quit()


def __init__(self):
    self.sec = 0
    self.min = 0
    self.hour = 0
    self.tg = 0
    self.total_time = (self.hour,self.min,self.sec)
    self.win()
    self.project_name()

#start window timer in thread
def start_t(self, widget):
    if self.tg == 0:
        self.tg = gobject.timeout_add(100, self.count)
#stop window timer
def stop_t(self, widget):
    if self.tg != 0:
        gobject.source_remove(self.tg)
        self.tg = 0
#Project Name pop up box
def project_name(self):
    self.projectName = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.projectName.set_position(gtk.WIN_POS_CENTER)
    self.projectName.set_size_request(280,160)
    self.text_entry = gtk.Entry()
    self.p_t = self.projectName.get_title()
    self.enter_button = gtk.Button("Enter")
    self.enter_button.connect("clicked", self.change_p_n)
    fixed = gtk.Fixed()
    fixed.put(self.text_entry, 50, 100)
    fixed.put(self.enter_button,210,100)
    self.projectName.add(fixed)
    self.projectName.show_all()
#change project name
def change_p_n(self,widget):
    self.timer_window.set_title(self.text_entry.get_text())
    self.projectName.destroy()

def save_box(self,widget):
    self.save = gtk.FileChooserDialog("Save As...", None,
    gtk.FILE_CHOOSER_ACTION_SAVE,
    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
    gtk.STOCK_SAVE, gtk.RESPONSE_OK))
    self.save.set_default_response(gtk.RESPONSE_OK)
    self.save.set_current_name(self.timer_window.get_title())
    self.f_filter = gtk.FileFilter()
    self.f_filter.set_name("Save Times")
    self.f_filter.add_pattern("*")
    self.save.add_filter(self.f_filter)
    self.response = self.save.run()

    if self.response == gtk.RESPONSE_OK:
        self.wbk = xlwt.Workbook()
        self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
        self.sheet.write(0,0,self.time1.get_text())
        self.sheet.write(0,1,self.time2.get_text())
        self.sheet.write(0,2,self.time3.get_text())
        self.wbk.save(self.save.get_filename())


#timer window
def win(self):
    self.timer_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.timer_window.set_position(gtk.WIN_POS_CENTER)
    self.timer_window.set_size_request(500,300)
    self.timer_window.set_title("Project Timers")
    self.button1 = gtk.Button("Pause")
    self.button1.set_tooltip_text("This will pause timer")
    self.button1.connect("clicked", self.stop_t)
    self.button2 = gtk.Button("Start")
    self.button2.set_tooltip_text("This will start timer")
    self.button2.connect("clicked", self.start_t)
    self.button3 = gtk.Button("Save")
    self.button3.set_tooltip_text("This will save your time to a excel file")
    self.button3.connect("clicked", self.save_box)
    self.time1 = gtk.Label("00")
    self.time1.modify_font(pango.FontDescription("40"))
    self.time2 = gtk.Label("00")
    self.time2.modify_font(pango.FontDescription("40"))
    self.time3 = gtk.Label("00")
    self.time3.modify_font(pango.FontDescription("30"))
    self.time4 = gtk.Label(":")
    self.time4.modify_font(pango.FontDescription("40"))
    self.time5 = gtk.Label(":")
    self.time5.modify_font(pango.FontDescription("40"))


    fixed = gtk.Fixed()
    fixed.put(self.button1, 30, 250)
    fixed.put(self.button2, 90, 250)
    fixed.put(self.button3, 150, 250)
    fixed.put(self.time1, 140, 100)
    fixed.put(self.time2, 215, 100)
    fixed.put(self.time3, 300, 110)
    fixed.put(self.time4, 280, 100)
    fixed.put(self.time5, 200, 100)
    self.timer_window.add(fixed)
    self.timer_window.show_all()


#timer count function
def count(self):
    self.sec += 1
    if self.sec >= 59:
        self.sec = 0
        self.min += 1
    if self.min >= 59:
        self.min = 0
        self.hour +=1

    self.time1.set_text(str(self.hour))
    self.time2.set_text(str(self.min))
    self.time3.set_text(str(self.sec))
    return True







if __name__ == "__main__":
timer1 = mainTimer()
timer1.main()

1 个答案:

答案 0 :(得分:0)

首先,您是否需要将这些文件保存为.xls文件而不是.csv文件?如果您需要CSV无法处理的功能(公式,图形等),显然您需要使用真实的电子表格,但对于一堆静态数据列,CSV更简单,并且Excel具有没问题。

但假设您确实需要使用.xls文件......


每次创建新文件的原因是您每次都明确告诉它创建新文件。这是您的代码:

self.wbk = xlwt.Workbook()
self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
self.sheet.write(0,0,self.time1.get_text())
self.sheet.write(0,1,self.time2.get_text())
self.sheet.write(0,2,self.time3.get_text())
self.wbk.save(self.save.get_filename())

这会创建一个新的Workbook,添加一个工作表,将三个单元格写入该工作表,然后保存。所以,显然,你得到一个全新的文件。


如果您不想要,请不要每次都创建新的工作簿和工作表。而且你还需要跟踪下一行,所以你不要一遍又一遍地覆盖相同的单元格。并且......那就是它。

例如:

def __init__(self):
    # ... existing code
    self.wbk = xlwt.Workbook()
    self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
    self.next_row = 0

def save_box(self,widget):
    # ... existing code
    if self.response == gtk.RESPONSE_OK:
        self.sheet.write(self.next_row, 0,self.time1.get_text())
        self.sheet.write(self.next_row, 1,self.time2.get_text())
        self.sheet.write(self.next_row, 2,self.time3.get_text())
        self.next_row += 1

或者,或者,不是每次都创建一个新工作表,而是每次重新打开同一个工作表并添加到它的末尾。