from tkinter import *
from tkinter import filedialog
import pandas as pd
import xlsxwriter
class App:
def __init__(self, master):
self.master = master
self.myfile = ''
self.initWidgets()
def initWidgets(self):
fm1 = Frame(self.master)
fm1.pack(side=LEFT, fill=BOTH, expand=YES)
lb1 = Label(fm1, text='1.第一步')
bt1 = Button(fm1, text='点击打开文件', command=self.open_filename)
self.st = StringVar()
lb11 = Label(fm1, text='', textvariable=self.st)
lb2 = Label(fm1, text='2.第二步')
bt2 = Button(fm1, text='点击开始处理',command=self.handle)
lb1.pack()
bt1.pack()
lb11.pack()
lb2.pack()
bt2.pack()
def open_filename(self):
# 调用askopenfilename方法获取单个文件的文件名
self.myfile = filedialog.askopenfilename(title='打开文件',
filetypes=[("excel文件", "*.xlsx"), ('excel文件', '*.xls')],
initialdir='d:\\')
self.st.set(self.myfile)
print(self.myfile)
def handle(self):
print('正在处理:\n{}'.format(self.myfile))
self.df = pd.read_excel(self.myfile)
self.rows = Tools.dftoarray(df=self.df)
Tools.writesheet(rows=self.rows, filename='newfile.xlsx')
class Tools:
@staticmethod
def dftoarray(df):
headline = pd.DataFrame(df.columns.values, index=df.columns).T
return pd.concat([headline, df]).values
@staticmethod
def writesheet(rows, filename, sheetname=None):
#workbook = xlsxwriter.Workbook(filename,{'strings_to_numbers':False})
workbook = xlsxwriter.Workbook(filename, {'nan_inf_to_errors': True})
cell_format = workbook.add_format({'font_name': 'Arial', 'font_size': '9'})
sheet = workbook.add_worksheet(sheetname)
sheet.set_column(0, 10, 12)
sheet.freeze_panes(1, 0)
for i, row in enumerate(rows):
sheet.write_row(i, 0, row, cell_format)
workbook.close()
root = Tk()
root.title("GUI实验")
display = App(root)
root.mainloop()
from tkinter import *
from tkinter import filedialog
import pandas as pd
import xlsxwriter
class App:
def __init__(self, master):
self.master = master
self.myfile = ''
self.initWidgets()
def initWidgets(self):
fm1 = Frame(self.master)
fm1.pack(side=LEFT, fill=BOTH, expand=YES)
lb1 = Label(fm1, text='1.第一步')
bt1 = Button(fm1, text='点击打开文件', command=self.open_filename)
self.st = StringVar()
lb11 = Label(fm1, text='', textvariable=self.st)
lb2 = Label(fm1, text='2.第二步')
bt2 = Button(fm1, text='点击开始处理',command=self.handle)
lb1.pack()
bt1.pack()
lb11.pack()
lb2.pack()
bt2.pack()
def open_filename(self):
# 调用askopenfilename方法获取单个文件的文件名
self.myfile = filedialog.askopenfilename(title='打开文件',
filetypes=[("excel文件", "*.xlsx"), ('excel文件', '*.xls')],
initialdir='d:\\')
self.st.set(self.myfile)
print(self.myfile)
def handle(self):
print('正在处理:\n{}'.format(self.myfile))
self.df = pd.read_excel(self.myfile)
self.rows = Tools.dftoarray(df=self.df)
Tools.writesheet(rows=self.rows, filename='备份.xlsx')
class Tools:
@staticmethod
def dftoarray(df):
headline = pd.DataFrame(df.columns.values, index=df.columns).T
return pd.concat([headline, df]).values
@staticmethod
def writesheet(rows, filename, sheetname=None):
#workbook = xlsxwriter.Workbook(filename,{'strings_to_numbers':False})
workbook = xlsxwriter.Workbook(filename, {'nan_inf_to_errors': True})
cell_format = workbook.add_format({'font_name': 'Arial', 'font_size': '9'})
sheet = workbook.add_worksheet(sheetname)
sheet.set_column(0, 10, 12)
sheet.freeze_panes(1, 0)
for i, row in enumerate(rows):
sheet.write_row(i, 0, row, cell_format)
workbook.close()
def main():
root = Tk()
root.title("GUI实验")
display = App(root)
root.mainloop()
if __name__ == '__main__':
main()