如何导出用户输入(蟒蛇)到Excel工作表?(How to export user inputs (

2019-06-25 13:32发布

我想开发蟒蛇2.7.3用户的形式。 请注意,我是一个Python初学者。

如何导出用户输入(蟒蛇)到Excel工作表?

谢谢

Answer 1:

对于你的写作形式,你可能希望使用Tk的-它的建成成Python( import Tkinter )。

对于导出到Excel,有几个选项:

  1. 您的数据写入到.csv文件( import csv ),然后用Excel加载
  2. 使用一个模块写入.xls文件
  3. 使用.COM自动化“远程控制”创先争优

编辑:好吧,这里使用的Tkinter和xlwt一个更具体的答案:

import Tkinter as tk
import xlwt
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="myfile.xls")
        self.sheet = tk.StringVar(value="sheet1")
        self.cell  = tk.StringVar(value="x1")
        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
        wb = xlwt.Workbook()

        # add sheet using given name
        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
        wb.save(self.fname.get())

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

if __name__=="__main__":
    main()


文章来源: How to export user inputs (from python) to excel worksheet?