python制作软件excel,利用Python制作Excel对比工具
目前功能还不完善,只能对比第一个sheet的内容,代码如下:
# -*- coding: utf-8 -*-
# 博客园 C丶小明
import xlrd,time
from docx import Document
from tkinter import *
from googletrans import Translator
import tkinter.messagebox
import tkinter.filedialog
class Comp:
def __init__(self,master):
self.master = master
def label(self,properties,padx = 3,pady = 2,ipadx = 5,ipady = 1):
for name,x,y in properties:
self.label = Label(self.master,text = name)
self.label.grid(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady)
def button(self,properties,padx = 1,pady = 2,ipadx = 2,ipady = 1):
for text,x,y,command in properties:
self.button = Button(self.master,text = text,command = command)
self.button.grid(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady)
class Tr:
def __init__(self):
self.main = Tk()
self.mainwindow = Comp(self.main)
self.main.geometry('454x400')
self.main.title(u"对比Excel工具-小明")
self.main.resizable(width=False,height=False)
# 对比excel内容部分
def Compared(self,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:
print ("当前对比Excel:n%sn%s"%(filename1,filename2),file = f)
print ("n对比内容为:行高,列宽,单元格合并,背景格式,字体信息,边框格式,单元格格式,内容nn对比异常结果如下:",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 = sheet1.nrows
cols = sheet1.ncols
# 判断两个excel行数和列数是否相等
if sheet1.nrows == sheet2.nrows and sheet1.ncols == sheet2.ncols:
# 对比合并单元格
hb1 = sheet1.merged_cells
hb2 = sheet2.merged_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):
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 in sheet1.rowinfo_map:
if sheet1.rowinfo_map[i].height != sheet2.rowinfo_map[i].height:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("行(%s),行高不同"%(i),file = f)
# 对比列宽(这个库有bug,默认的列宽会获取不到)
lk1 = []
lk2 = []
if list(sheet1.colinfo_map.keys()) == list(sheet2.colinfo_map.keys()):
for i in sheet1.colinfo_map:
if sheet1.colinfo_map[i].width != sheet2.colinfo_map[i].width:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("列(%s),列宽不同"%(i),file = f)
else:
for i in sheet1.colinfo_map:
for j in sheet2.colinfo_map:
if i == j:
lk1.append(i)
for j in (list(sheet1.colinfo_map.keys()) + list(sheet2.colinfo_map.keys())):
if j not in lk1:
lk2.append(j)
for k in lk1:
if sheet1.colinfo_map[k].width != sheet2.colinfo_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):
for col in range(0,cols):
xfx1 = sheet1.cell_xf_index(row, col)
xfx2 = sheet2.cell_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 = sheet1.cell_value(row, col)
e2 = sheet2.cell_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_set != b2.character_set or b1.colour_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 != b2.italic or b1.name != b2.name or b1.outline != b2.outline or b1.shadow != b2.shadow or b1.struck_out != b2.struck_out or b1.underline_type != b2.underline_type or b1.underlined != b2.underlined or b1.weight != b2.weight:
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 c1.diag_up != c2.diag_up or c1.left_colour_index != c2.left_colour_index or c1.left_line_style != c2.left_line_style or c1.right_colour_index != c2.right_colour_index or c1.right_line_style != c2.right_line_style or c1.top_colour_index != c2.top_colour_index or c1.top_line_style != c2.top_line_style:
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 or d1.rotation != d2.rotation or d1.shrink_to_fit != d2.shrink_to_fit or d1.text_direction != d2.text_direction or d1.text_wrapped != d2.text_wrapped or d1.vert_align != d2.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)
else:
with open(r"%s/%s.txt"%(savefile,Logtime),"a+") as f:
print ("两个excel获取到的最大行和列不相同,n%s行数为:%s,列数为:%sn%s行数为:%s,列数为:%s"%(filename1,sheet1.nrows,sheet1.ncols,filename2,sheet2.nrows,sheet2.ncols),file = f)
# 界面部分
def interface(self):
# 文本
self.mainwindow.label([
("Excel路径一:",0,0),
("Excel路径二:",1,0),
("结果存放路径:",2,0)
],ipady = 10,ipadx = 8)
path = StringVar()
path1 = StringVar()
savepath = StringVar()
def selectPath():
# 文件选择框
path_ = tkinter.filedialog.askopenfilename(filetypes=[("",".xls")])
# path = StringVar() 配合使用,更新显示地址
path.set(path_)
# 选择文档的路径
self.t = path_
def selectPath1():
# 文件选择框
path1_ = tkinter.filedialog.askopenfilename(filetypes=[("",".xls")])
# path = StringVar() 配合使用,更新显示地址
path1.set(path1_)
# 选择文档的路径
self.t1 = path1_
def savePath():
# 文件选择框
savepath_ = tkinter.filedialog.askdirectory(initialdir='C:/')
# path = StringVar() 配合使用,更新显示地址
savepath.set(savepath_)
# 选择文档的路径
self.t2 = savepath_
def start():
try:
self.Compared(self.t,self.t1,self.t2)
tkinter.messagebox.showinfo('提示','对比成功!')
except:
tkinter.messagebox.showinfo('提示','对比出错,请先正确选择路径或文件内容!')
# 显示选择文件地址
Label(self.main,width =35,height = 3,wraplength = 230,textvariable = path).grid(row = 0,column = 1,padx = 8)
Label(self.main,width =35,height = 3,wraplength = 230,textvariable = path1).grid(row = 1,column = 1,padx = 8)
Label(self.main,width =35,height = 3,wraplength = 230,textvariable = savepath).grid(row = 2,column = 1,padx = 8)
# 按钮
self.mainwindow.button([
("选择",0,2,selectPath),
("选择",1,2,selectPath1),
("选择",2,2,savePath),
("开始对比",3,1,start)
],ipadx = 8,ipady = 1)
self.main.mainloop()
if __name__ == "__main__":
t = Tr()
t.interface()
准备两个需要对比的Excel:
运行后,选择需要对比的Excel文件和结果存放路径,点击开始对比:
结果:
内容来源于网络如有侵权请私信删除
python制作软件excel,利用Python制作Excel对比工具相关推荐
- python制作软件excel_利用Python制作一个 截图+Excel操作浏览器小工具
代码如下: GetData.py import xlrd class ReadExcel(): def __init__(self,file): self.open_excel = xlrd.open ...
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名.合并两个不同路径下图片文件名等目录/路径案例.正确加载图片路径)之详细攻略 目录 利用python ...
- Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法
Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)-命令提示符cmd的几种方法 目录 利用python语言实现调用内部命令-命令提示符cmd的几种方法 T1. ...
- 用python做数据分析pdf_利用python进行数据分析pdf
利用python进行数据分析pdf微盘下载!<利用python进行数据分析>利用Python实现数据密集型应用由浅入深帮助读者解决数据分析问题~适合刚刚接触Python的分析人员以及刚刚接 ...
- Python之ffmpeg:利用python编程基于ffmpeg将m4a格式音频文件转为mp3格式文件
Python之ffmpeg:利用python编程基于ffmpeg将m4a格式音频文件转为mp3格式文件 目录 利用python编程基于ffmpeg将m4a格式音频文件转为mp3格式文件 1.先下载ff ...
- 怎样在php中制作电子相册,免费电子相册制作软件 怎样把照片制作成电子相册并加背景音乐?...
免费电子相册制作软件 怎样把照片制作成电子相册并加背景音乐? 最近这几天某明星去世的消息可是传遍了哈,虽然不粉他,但是年纪轻轻就这样结束了生命还是觉得很可惜,关键粉丝也很是难过啊,每个人都有自己心爱的 ...
- 手机版python编程软件下载,手机python编程软件
1.求 python 64位安装包下载 软件介绍: python是一款面向对象.解释型.动态数据类型的高级编程设计语言.它拥有语言上的简洁性.可读性和易维护性,在图形处理.数学处理.文本处理.系统编程 ...
- python制作电脑软件_利用PYTHON制作桌面版爬虫软件(二)
今天继续新的专题.主要讲解[利用PYTHON制作桌面版爬虫软件]下的如何实现界面功能(一).该讲主要包括以下三个内容:掌握如何编写主函数,运行界面. 了解pywin32模块. 如何用python识别Q ...
- python制作电脑软件_利用PYTHON制作桌面版爬虫软件(一)
抱歉,对长沙房地产数据的挖掘与分析[三],想了蛮久,觉得对自己的分析结果不是很理想.等我完善好了,我再发出来吧.今天继续开启新的一专题.主要讲解如何用PYTHON实现简单的桌面软件的制作. 题外话,我 ...
- python 渐变色柱形图_Python利用imshow制作自定义渐变填充柱状图(colorbar)
目的 在各种各样的理论计算中,常常需要绘制各种填充图,绘制完后需要加渐变填充的colorbar.可是有些软件如VMD,colorbar渲染后颜色分布有些失真,不能较准确的表达各颜色对应的数值.用ps中 ...
最新文章
- python[外星人入侵] 项目
- 简历包装要避开哪些坑,资深面试官告诉你!
- Java性能调优实践
- php变量定义的位置,php变量一般放在哪个位置
- 【原创】Struts2.5.12版本中使用通配符*
- DM8168学习--引导顺序
- Typescript 学习笔记七:泛型
- web前端培训分享Electron之Main Process API
- CentOS6.7 时间同步
- 如何升级浏览器_前谷歌员工爆料:谷歌工程师们是如何合谋“杀死”IE6浏览器的...
- 解题报告 Lights
- day23 java的异常
- Golang Json文件解析为结构体工具-json2go
- 服务器系统 比较,各种服务器操作系统比较
- linux完全卸载mysql_mysql完全卸载教程(图文详细)
- JAVAFX版简易计算器
- stc15f2k60f2单片机定时器_8 STC15F2K60S2单片机的定时器计数器 例题
- java输出hello word,新手求教如何输出helloword
- 大数据工程师就业前景及发展方向分析
- win10进入安全模式和退出安全模式
热门文章
- i春秋python_i春秋CTF web题(1)
- 贾天昊 - Nick
- FFmpeg----图片处理
- 局域网arp攻击_一篇文章告诉你,什么是ARP协议与ARP欺骗
- 杰理AP之PO声处理【篇】
- html5霓虹效果代码,纯CSS实现酷炫的霓虹灯效果(附demo)
- Logicalis宣布首席执行官过渡计划
- Ubuntu16.04 Caffe系列 最新更新安装教程 附NVIDIA显卡安装及cuda、cudnn、caffe的安装
- javaweb项目图书借阅管理系统设计与实现(有文档+调试视频教程+项目源码).rar
- 七日杀服务器进不去显示红字,七日杀各类红字问题解决方案 七日杀红字怎么办...