由于业务需要,VBA又不适合于不熟悉代码的同事使用 因此将原来的VBA代码改成Python代码并封装,后续有机会在进行改善 完整代码 import xlrd import xlwt from xlutils.copy import copy from tkinter im
由于业务需要,VBA又不适合于不熟悉代码的同事使用
因此将原来的VBA代码改成Python代码并封装,后续有机会在进行改善
完整代码
import xlrd import xlwt from xlutils.copy import copy from tkinter import * def run1(): qt1,qt2 = 0,0 #打开表格 try: workbook = xlrd.open_workbook(r‘.\\fendan.xlsx‘) except: txt.insert(END, ‘打开表格失败,请检查是否已创建表格fendan.xlsx‘) txt.insert(END, ‘\n‘) #获取名称 sheet_names = workbook.sheet_names() #获取对象 sheet1 = workbook.sheet_by_name(‘Sheet1‘) ##wb = xlwt.Workbook(r‘.\\fendan.xlsx‘) ##ws = wb.get_sheet(0) write_data = copy(workbook) write_save = write_data.get_sheet(0) nrows = sheet1.nrows #前处理,将订单号取消尾数 for i in range(1,nrows): if not sheet1.cell_value(rowx=i,colx=1)[:10] ==‘‘: ## print(sheet1.cell_value(rowx=i,colx=1)[:10]) #取第i行2列前10个数写入i行3列 write_save.write(i,2,sheet1.cell_value(rowx=i,colx=1)[:10]) write_data.save(‘fendan2.xlsx‘) #打开表格 workbook2 = xlrd.open_workbook(r‘.\\fendan2.xlsx‘) #获取对象 sheet2 = workbook2.sheet_by_name(‘Sheet1‘) write_data2 = copy(workbook2) write_save2 = write_data2.get_sheet(0) #设置写入样式1 font0 = xlwt.Font() pattern1 = xlwt.Pattern() pattern1.pattern = xlwt.Pattern.SOLID_PATTERN pattern1.pattern_fore_colour = 5 style0 = xlwt.XFStyle() style0.pattern = pattern1 #设置写入样式2 pattern2 = xlwt.Pattern() pattern2.pattern = xlwt.Pattern.SOLID_PATTERN pattern2.pattern_fore_colour = 5 pattern2.pattern_fore_colour = 2 style1 = xlwt.XFStyle() style1.pattern = pattern2 nrows = sheet2.nrows for k in range(1,nrows): for r in range(1,nrows): if not sheet2.cell_value(rowx=k,colx=2) == ‘‘: ## print(k,r) if sheet2.cell_value(rowx=r,colx=2) == sheet2.cell_value(rowx=k,colx=5): ## print(sheet2.cell_value(rowx=r,colx=2),sheet2.cell_value(rowx=k,colx=5)) ## print(sheet2.cell_value(rowx=r,colx=2) == sheet2.cell_value(rowx=k,colx=5)) if sheet2.cell_value(rowx=r,colx=3) == sheet2.cell_value(rowx=k,colx=6): ## print(sheet2.cell_value(rowx=r,colx=3),sheet2.cell_value(rowx=k,colx=6)) ## print(sheet2.cell_value(rowx=r,colx=3) == sheet2.cell_value(rowx=k,colx=6)) write_save2.write(r,2,sheet2.cell_value(rowx=r,colx=2),style=style0) write_save2.write(r,3,sheet2.cell_value(rowx=r,colx=3),style=style0) write_save2.write(k,5,sheet2.cell_value(rowx=k,colx=5),style=style1) write_save2.write(k,6,sheet2.cell_value(rowx=k,colx=6),style=style1) qt1 = qt1 +sheet2.cell_value(rowx=r,colx=3) qt2 = qt2 +sheet2.cell_value(rowx=k,colx=6) ## print(sheet1.row_values(rowx=r,start_colx=2,end_colx=7)) write_data2.save(‘fendan2.xlsx‘) ## print(qt1,qt2) txt.insert(END, ‘区分完毕‘) txt.insert(END, ‘\n‘) txt.insert(END, (qt1,qt2)) txt.insert(END, ‘\n‘) root = Tk() root.title(‘区分订单‘) root.geometry(‘460x300‘) lb1 = Label(root, text=‘请在Excel表格中分别输入第三、四、六、七列订单与数量‘) lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1) btn1 = Button(root, text=‘输入完请关闭Excel表格,点击此按钮‘, command=run1) btn1.place(relx=0.2, rely=0.2, relwidth=0.6, relheight=0.3) txt = Text(root) txt.place(rely=0.6, relheight=0.3) lb2 = Label(root, text=‘作者:YBX‘) lb2.place(relx=0.8, rely=0.9, relwidth=0.2, relheight=0.1) root.mainloop()制作Excel表格校对两列数字函数并封装为EXE
对以下参考资料表示感谢:
https://www.jianshu.com/p/91844c5bca78
https://www.cnblogs.com/jackzz/p/9431923.html