Python之xlwings+tkinter简单小应用

# -*- coding: cp936 -*-
import os
import re
import sys
import xlwings as xw
from tkinter import *
from tkinter import filedialog
from tkMessageBox import *

FILE1=""
FILE2=""

def callback1():
    global FILE1
    FILE1=filedialog.askopenfilename()
    filename1 = os.path.basename(FILE1)
    text1.delete(0.0, 'end')
    text1.insert("insert",filename1)
    #print(FILE1)

def callback2():
    global FILE2
    FILE2=filedialog.askopenfilename()
    filename2 = os.path.basename(FILE2)
    text2.delete(0.0, 'end')
    text2.insert("insert",filename2)
    #print(FILE2)

def callback3():
    global FILE1
    global FILE2

    if FILE1 == "" or FILE2 == "":
        showinfo(message="Please input file!")
    else:
        filename1 = os.path.basename(FILE1)
        filename2 = os.path.basename(FILE2)

        r1 = re.findall(r'\d\d\d\d\d\d', filename1)

        r2 = re.findall(r'\d\d\d\d\d\d', filename2)

        FILE3 = os.path.dirname(FILE1)+'/'+"".join(r1)+'.py'+'.xls'
        #print(FILE3)

        if r1 == r2:
            app = xw.App(visible=False, add_book=False)

            wb1 = app.books.open(FILE1)
            sht1 = wb1.sheets[0]

            wb2 = app.books.open(FILE2)
            sht2 = wb2.sheets[0]

            wb3 = app.books.add()
            sht3 = wb3.sheets[0]

            sht3.range('C' + '3').value = filename1
            sht3.range('E' + '3').value = filename2
            sht3.range('G' + '3').value = filename2 + "-" + filename1

            sht3.range('A' + '4').value = "项目"
            sht3.range('B' + '4').value = "行次"
            sht3.range('C' + '4').value = "本月数"
            sht3.range('D' + '4').value = "本年累计数"
            sht3.range('E' + '4').value = "本月数"
            sht3.range('F' + '4').value = "本年累计数"
            sht3.range('G' + '4').value = "本年累计数"

            start_row = 5
            end_row = 35

            for row in range(start_row, end_row):
                row_str = str(row)

                #print("%02d"%(row-4)),
                #print("%12.2f"%sht1.range('C' + row_str).value),
                #print("%12.2f"%sht1.range('D' + row_str).value),
                #print("%12.2f"%sht2.range('C' + row_str).value),
                #print("%12.2f"%sht2.range('D' + row_str).value),
                #print("")

                #sht3.range('A' + row_str).value = row-4
                
                sht3.range('A' + row_str).value = sht1.range('A' + row_str).value
                sht3.range('B' + row_str).value = row-4
                sht3.range('C' + row_str).value = sht1.range('C' + row_str).value
                sht3.range('D' + row_str).value = sht1.range('D' + row_str).value
                sht3.range('E' + row_str).value = sht2.range('C' + row_str).value
                sht3.range('F' + row_str).value = sht2.range('D' + row_str).value
                sht3.range('G' + row_str).value = sht2.range('D' + row_str).value - sht1.range('D' + row_str).value

            wb3.save(FILE3)
            
            wb1.close()
            wb2.close()
            wb3.close()

            showinfo(message="Execute success!")
        else:
            showinfo(message="File is different!")

root=Tk()
root.title('ALingT-xlbtlmy')
root.geometry('320x150')
root.resizable(False, False)

Button(root, text='Open File1', command=callback1).pack()

text1 = Text(root, width=30, height=1)
text1.pack()

Button(root, text='Open File2', command=callback2).pack()

text2 = Text(root, width=30, height=1)
text2.pack()

Button(root, text='execute', command=callback3).pack()

mainloop()

运行一下脚本生成exe程序

@echo off

SET PATH_Python27="D:\Python27"

SET PATH=%PATH_Python27%;%PATH%

pyinstaller --onefile -w -F %1

pause

生成的界面如下:
Python之xlwings+tkinter简单小应用_第1张图片

# -*- coding: cp936 -*-
import os
import re
import sys
import xlwings as xw
from tkinter import *
from tkinter import filedialog
from tkMessageBox import *

FILE1=""
FILE2=""

def callback1():
    global FILE1
    FILE1=filedialog.askopenfilename()
    filename1 = os.path.basename(FILE1)
    Text1.delete(0, END)
    Text1.insert(0,filename1)
    #print(FILE1)

def callback2():
    global FILE2
    FILE2=filedialog.askopenfilename()
    filename2 = os.path.basename(FILE2)
    Text2.delete(0, END)
    Text2.insert(0,filename2)
    #print(FILE2)

def callback3():
    global FILE1
    global FILE2

    if FILE1 == "" or FILE2 == "":
        showinfo(message="Please input file!")
    else:
        filename1 = os.path.basename(FILE1)
        filename2 = os.path.basename(FILE2)

        r1 = re.findall(r'\d\d\d\d\d\d', filename1)

        r2 = re.findall(r'\d\d\d\d\d\d', filename2)

        FILE3 = os.path.dirname(FILE1)+'/'+"".join(r1)+'.py'+'.xls'
        #print(FILE3)

        if r1 == r2:
            app = xw.App(visible=False, add_book=False)

            wb1 = app.books.open(FILE1)
            sht1 = wb1.sheets[0]

            wb2 = app.books.open(FILE2)
            sht2 = wb2.sheets[0]

            wb3 = app.books.add()
            sht3 = wb3.sheets[0]

            sht3.range('C' + '3').value = filename1
            sht3.range('E' + '3').value = filename2
            sht3.range('G' + '3').value = filename2 + "-" + filename1

            sht3.range('A' + '4').value = "项目"
            sht3.range('B' + '4').value = "行次"
            sht3.range('C' + '4').value = "本月数"
            sht3.range('D' + '4').value = "本年累计数"
            sht3.range('E' + '4').value = "本月数"
            sht3.range('F' + '4').value = "本年累计数"
            sht3.range('G' + '4').value = "本年累计数"

            start_row = 5
            end_row = 35

            for row in range(start_row, end_row):
                row_str = str(row)

                #print("%02d"%(row-4)),
                #print("%12.2f"%sht1.range('C' + row_str).value),
                #print("%12.2f"%sht1.range('D' + row_str).value),
                #print("%12.2f"%sht2.range('C' + row_str).value),
                #print("%12.2f"%sht2.range('D' + row_str).value),
                #print("")

                #sht3.range('A' + row_str).value = row-4
                
                sht3.range('A' + row_str).value = sht1.range('A' + row_str).value
                sht3.range('B' + row_str).value = row-4
                sht3.range('C' + row_str).value = sht1.range('C' + row_str).value
                sht3.range('D' + row_str).value = sht1.range('D' + row_str).value
                sht3.range('E' + row_str).value = sht2.range('C' + row_str).value
                sht3.range('F' + row_str).value = sht2.range('D' + row_str).value
                sht3.range('G' + row_str).value = sht2.range('D' + row_str).value - sht1.range('D' + row_str).value

            wb3.save(FILE3)
            
            wb1.close()
            wb2.close()
            wb3.close()

            showinfo(message="Execute success!")
        else:
            showinfo(message="File is different!")

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master,bg='gray')
        self.pack(expand=YES,fill=BOTH)
        self.window_init()
        self.createWidgets()
    
    def window_init(self):
        self.master.title('AlingT-xlbtlmy')
        #self.master.bg='black'
        width = 270
        height = 60
        self.master.geometry("{}x{}".format(width, height))

root=Tk()
root.title('ALingT-xlbtlmy')
root.geometry('270x60')
root.resizable(False, False)

fm2=Frame()
fm2_left=Frame(fm2)
fm2_right=Frame(fm2)
fm2_left_top=Frame(fm2_left)
fm2_left_bottom=Frame(fm2_left)

Button1=Button(fm2_left_top,text='open file1',command=callback1)
Button1.pack(side=LEFT)
Text1=Entry(fm2_left_top)
Text1.pack(side=LEFT)
fm2_left_top.pack(side=TOP)

Button2=Button(fm2_left_bottom,text='open file2',command=callback2)
Button2.pack(side=LEFT)
Text2=Entry(fm2_left_bottom)
Text2.pack(side=LEFT)
fm2_left_bottom.pack(side=TOP)

fm2_left.pack(side=LEFT)
Button3=Button(fm2_right,text='execute',command=callback3)
Button3.pack(side=LEFT, fill="both")
fm2_right.pack(side=LEFT, fill="both")

fm2.pack(side=TOP)

root.mainloop()

在这里插入图片描述

你可能感兴趣的:(Python)