利⽤Python制作Excel对⽐⼯具⽬前功能还不完善,只能对⽐第⼀个sheet的内容,代码如下:
# -*- coding: utf-8 -*-
# 博客园 C⼂⼩明
import xlrd,time
from docx import Document
from tkinter import *
from googletrans import Translator
ssagebox
import tkinter.filedialog
class Comp:
def __init__(lf,master):
lf.master = master
def label(lf,properties,padx = 3,pady = 2,ipadx = 5,ipady = 1):
for name,x,y in properties:
lf.label = Label(lf.master,text = name)
id(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady)
def button(lf,properties,padx = 1,pady = 2,ipadx = 2,ipady = 1):
for text,x,y,command in properties:
lf.button = Button(lf.master,text = text,command = command)
id(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady)
class Tr:
def __init__(lf):
lf.main = Tk()
lf.mainwindow = Comp(lf.main)
ry('454x400')
lf.main.title(u"对⽐Excel⼯具-⼩明")
sizable(width=Fal,height=Fal)
# 对⽐excel内容部分
def Compared(lf,filename1,filename2,savefile):
Logtime = time.strftime('%Y-%m-%d %H%M%S', time.localtime(time.time()))
with open(r"%s/%s.txt"%(savefile,Logtime),"w") as f:
huaer
print ("当前对⽐Excel:\n%s\n%s"%(filename1,filename2),file = f)
print ("\n对⽐内容为:⾏⾼,列宽,单元格合并,背景格式,字体信息,边框格式,单元格格式,内容\n\n对⽐异常结果如下:",file = f)
book1 = xlrd.open_workbook(filename1,formatting_info=True)
sheet1 = book1.sheet_by_index(0)
book2 = xlrd.open_workbook(filename2,formatting_info=True)
sheet2 = book2.sheet_by_index(0)
rows = ws
cols = ls
# 判断两个excel⾏数和列数是否相等
ws == ws ls == ls:
# 对⽐合并单元格
hb1 = d_cells
hb2 = d_cells
hb3 = []
hb4 = []
if hb1 != hb2:
for i in hb1:
for j in hb2:
if i == j:
hb3.append(i)
for i in (hb1 + hb2):attack什么意思
if i not in hb3:
hb4.append(i)
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s),单元格合并不同"%(hb4),file = f)
# 对⽐⾏⾼
for i winfo_map:
winfo_map[i].height != winfo_map[i].height:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("⾏(%s),⾏⾼不同"%(i),file = f)
# 对⽐列宽(这个库有bug,默认的列宽会获取不到)
lk1 = []
lk2 = []
if linfo_map.keys()) == linfo_map.keys()):
for i linfo_map:
linfo_map[i].width != linfo_map[i].width:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("列(%s),列宽不同"%(i),file = f)
el:
for i linfo_map:
for j linfo_map:
if i == j:
lk1.append(i)
for j in (linfo_map.keys()) + linfo_map.keys())):
if j not in lk1:
lk2.append(j)
for k in lk1:
linfo_map[k].width != linfo_map[k].width:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("列(%s),列宽不同"%(i),file = f)
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("列%s,列宽不同"%(lk2),file = f)
# 循环对⽐最⼤⾏和列
for row in range(0,rows):
trimleftfor col in range(0,cols):
xfx1 = ll_xf_index(row, col)
xfx2 = ll_xf_index(row, col)
xf1 = book1.xf_list[xfx1]
xf2 = book2.xf_list[xfx2]
# 背景格式
a1 = xf1.background
a2 = xf2.background
# 字体
b1 = book1.font_list[xf1.font_index]
b2 = book2.font_list[xf2.font_index]
# 边框格式
c1 = xf1.border
c2 = xf2.border
# 单元格格式
d1 = xf1.alignment
d2 = xf2.alignment
# 内容
e1 = ll_value(row, col)
e2 = ll_value(row, col)
# 对⽐
if a1.background_colour_index != a2.background_colour_index or a1.fill_pattern != a2.fill_pattern or a1.pattern_colour_index != a2.pattern_colour_index:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s,%s),背景格式不同"%(row,col),file = f)
if b1.bold != b2.bold or b1.character_t != b2.character_t lour_index != b2.colour_index or b1.escapement != b2.escapement or b1.family != b2.family or b1.font_index != b2.font_index or b1.height != b2.height or b1.italic with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s,%s),字体信息不同"%(row,col),file = f)
if c1.bottom_colour_index != c2.bottom_colour_index or c1.bottom_line_style != c2.bottom_line_style or c1.diag_colour_index != c2.diag_colour_index or c1.diag_down != c2.diag_down or c1.diag_line_style != c2.diag_line_style or c with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s,%s),边框格式不同"%(row,col),file = f)
if d1.hor_align != d2.hor_align or d1.indent_level != d2.indent_level ation != d2.rotation or d1.shrink_to_fit != d2.shrink_to_fit _direction != d2.text_direction _wrapped != d2.text_wrapped or d1.vert_align != with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s,%s),单元格格式不同"%(row,col),file = f)
if e1 != e2:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("单元格(%s,%s),内容不同"%(row,col),file = f)
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("\n***对⽐完成***",file = f)
el:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("两个excel获取到的最⼤⾏和列不相同,\n%s⾏数为:%s,列数为:%s\n%s⾏数为:%s,列数为:%s"%(ls,ls),file = f)
wap什么意思
# 界⾯部分
streer
def interface(lf):
# ⽂本
lf.mainwindow.label([
("Excel路径⼀:",0,0),
("Excel路径⼆:",1,0),
宇多田光 first love("结果存放路径:",2,0)
],ipady = 10,ipadx = 8)
path = StringVar()
path1 = StringVar()
savepath = StringVar()
def lectPath():
# ⽂件选择框
path_ = tkinter.filedialog.askopenfilename(filetypes=[("",".xls")])
# path = StringVar() 配合使⽤,更新显⽰地址
path.t(path_)
# 选择⽂档的路径
lf.t = path_
def lectPath1():
takeabow
# ⽂件选择框
path1_ = tkinter.filedialog.askopenfilename(filetypes=[("",".xls")])
# path = StringVar() 配合使⽤,更新显⽰地址
path1.t(path1_)
# 选择⽂档的路径
lf.t1 = path1_
def savePath():
flowable# ⽂件选择框
savepath_ = tkinter.filedialog.askdirectory(initialdir='C:/')
# path = StringVar() 配合使⽤,更新显⽰地址
savepath.t(savepath_)
# 选择⽂档的路径
lf.t2 = savepath_
def start():
try:
lf.Compared(lf.t,lf.t1,lf.t2)
etc什么意思
except:
# 显⽰选择⽂件地址
Label(lf.main,width =35,height = 3,wraplength = 230,textvariable = path).grid(row = 0,column = 1,padx = 8)
Label(lf.main,width =35,height = 3,wraplength = 230,textvariable = path1).grid(row = 1,column = 1,padx = 8)
Label(lf.main,width =35,height = 3,wraplength = 230,textvariable = savepath).grid(row = 2,column = 1,padx = 8)
重庆瑜伽培训
# 按钮
lf.mainwindow.button([
("选择",0,2,lectPath),
("选择",1,2,lectPath1),
("选择",2,2,savePath),
("开始对⽐",3,1,start)
],ipadx = 8,ipady = 1)
lf.main.mainloop()
if __name__ == "__main__":
t = Tr()
t.interface()
准备两个需要对⽐的Excel:
运⾏后,选择需要对⽐的Excel⽂件和结果存放路径,点击开始对⽐:结果: