如何使用python2.7的raw_input函数在现有travel order.xlsx文件的单元格中写入字符串s和整数值?

时间:2017-11-30 13:44:45

标签: python-2.7 openpyxl xlrd xlsxwriter

下面是我从这里复制的代码,我想问一下我应该使用什么来加载现有的excel文件(.xlsx)并将数据写入该excel文件(.xlsx)的命名工作表的各个单元格中将数据保存为其他文件名?

import Tkinter as tk
import xlwt
import openpyxl
import xlsxwriter
from xlwt.Utils import cell_to_rowcol2

class MyForm(tk.Frame):
def __init__(self, master=None, cnf={}, **kw):
    tk.Frame.__init__(self, master, cnf, **kw)

    self.fname = tk.StringVar(value="travelorder.xlsx")
    self.sheet = tk.StringVar(value="DOMESTIC")
    self.cell  = tk.StringVar(value="c4")
    self.value = tk.StringVar(value="1234")

    tk.Label(master, text="File").grid(row=0, column=0, sticky=tk.E)
    tk.Entry(master, textvariable=self.fname).grid(row=0, column=1, padx=4, 
    pady=4)

    tk.Label(master, text="Sheet").grid(row=1, column=0, sticky=tk.E)
    tk.Entry(master, textvariable=self.sheet).grid(row=1, column=1, padx=4, 
    pady=4)

    tk.Label(master, text="Cell").grid(row=2, column=0, sticky=tk.E)
    tk.Entry(master, textvariable=self.cell).grid(row=2, column=1, padx=4, 
    pady=4)

    tk.Label(master, text="Value").grid(row=3, column=0, sticky=tk.E)
    tk.Entry(master, textvariable=self.value).grid(row=3, column=1, padx=4, 
    pady=4)

    go = tk.Button(master, text="Do it!", 
    command=self.write_to_xls).grid(row=4, column=1, padx=4, pady=4, 
    sticky=tk.W)
    self.grid()

    def write_to_xls(self):
       # create new workbook
       xfile = openpyxl.load_workbook('travelorder.xlsx')

    # add sheet using given name
    ws= xfile.get_sheet_by_name(self.sheet.get())
    # ws = wb.add_sheet(self.sheet.get())
    # get offset of cell to write to
    row,col = cell_to_rowcol2(self.cell.get())

    # write text to cell
    ws.write(row, col, self.value.get())

    # save to given file name
    xfileout=raw_input('Enterfilename to save:')
    xfile.save(xfileout)

def main():
master = tk.Tk()
myform = MyForm(master)
tk.mainloop()

if __name__=="__main__":
   main()

我想建立一个GUI,用户将在其中输入数据,这些数据将写入已存在的.xlsx文件中特定工作表中的特定单元格。

请帮助..

0 个答案:

没有答案