#-*- coding: utf-8 -*-

#比对两个Excel文件内容的差异
#---------------------假设条件----------------
#1、源表和目标表格式一致
#2、不存在合并单元格
#3、第2行开始比对
#--------------------------------------------- import xlrd
import xlwt
import os
import time; # 引入time模块 #往日志文件中追加内容函数
def writeappend_logfile(filename,content):
file=open(filename,'a') #以追加方式打开日志文件
time_now= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #系统时间格式化
file.writelines(time_now+':'+content+'\n') #写入内容
file.close() #关闭文件 def read_excel(ori_path,tar_path,sub_name):#
#print("ori_path:", ori_path)
#print("tar_path:", tar_path)
success=0 #匹配一致数量
fail=0 #匹配不一致数量
origin_xls={} #存储源xls文件
target_xls={} #比对的xls文件
wb_ori=xlrd.open_workbook(ori_path) #打开原始文件
wb_tar=xlrd.open_workbook(tar_path) #打开目标文件
sheet_num = len(wb_ori.sheets()) #源表子表数量
## for sheet_i in range(sheet_num): #excel中子页面数量
## sheet_ori=wb_ori.sheet_by_index(sheet_i) #通过索引值获取源表名
## sheet_tar=wb_tar.sheet_by_index(sheet_i) #通过索引值获取源表名 startime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #获取系统当前时间并格式化为格式
print (startime,' 开始比对...')
logname='log_'+startime[0:10]+'.log' #截取日期年月日构成日志文件名 logfile=open(logname,'w') #创建日志文件,如果文件存在则清空内容,不存在则创建,如果需要同时批量比对多张表,可以考虑将日志文件名作为参数传入
logfile.writelines(startime+':【开始比对】...'+'\n') #写入开始时间
logfile.close() #关闭日志文件
#print("##########################") try:
sheet_ori=wb_ori.sheet_by_name(sub_name)
#print("sheet_ori.name:", sheet_ori.name)
sheet_tar=wb_tar.sheet_by_name(sub_name)
#print("sheet_tar.name:", sheet_tar.name)
#print("1111111111111111111111111111111")
if sheet_ori.name==sheet_tar.name:
#sheet表名
if sheet_ori.name==sub_name:
#先将数存入dictionary中dictionary(rows:list)
#第一行存储表头
#源表取一行数据与目标表全表进行比对如果表中存在主键可以用主键进行索引
#数据从excel第3行开始
#print("222222222222222222222222222")
for rows in range(0,sheet_ori.nrows):
orign_list=sheet_ori.row_values(rows) #源表i行数据
#target_list=sheet_tar.row_values(rows) #目标表i行数据
origin_xls[rows]=orign_list #源表写入字典
#print("origin_xls[rows]:", origin_xls[rows])
#target_xls[rows]=target_list #目标表写入字典
for rows in range(0, sheet_tar.nrows):
target_list = sheet_tar.row_values(rows) # 目标表i行数据
target_xls[rows] = target_list # 目标表写入字典
#print("target_xls[rows]", target_xls[rows]) if origin_xls[0] == target_xls[0]:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 表头一致')
num = len(origin_xls)
print("num:", num)
num1 = len(target_xls)
print("num1:", num1)
if num >= num1:
for ori_num in origin_xls:
print("ori_num:", ori_num)
flag='false' #判断是否一致标志
for tar_num in target_xls:
if origin_xls[ori_num]==target_xls[tar_num]:
flag='true'
break #如果匹配到结果退出循环
if flag=='true': #匹配上结果输出后台日志
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 文件:', ori_path+' '+' row:%d is ok'%(ori_num+1))
success+=1
else: #匹配不上将源表中行记录写入txt
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 文件:', ori_path+' '+' row:%d is different'%(ori_num+1))
fail+=1
data=origin_xls[ori_num]
logstr='文件:', ori_path + ' ' + '【不一致】row<'+str(ori_num)+'>:'+str(data)
writeappend_logfile(logname,logstr)
# logstr='【比对完成】总记录数:'+str(ori_num)+'条,一致:'+str(success)+'条,不一致:'+str(fail)+'条'
logstr='【比对完成】总记录数:{:d}条,一致:{:d}条,不一致:{:d}条'.format(ori_num + 1,success,fail)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 【%s】比对结束'%sheet_ori.name)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 总记录数:%d条,一致:%d条,不一致:%d条'%(ori_num+1,success,fail))
writeappend_logfile(logname,logstr)
else:
for tar_num in target_xls:
#for ori_num in origin_xls:
#print("tar_num:", tar_num)
flag = 'false' # 判断是否一致标志
#for tar_num in target_xls:
for ori_num in origin_xls:
#if origin_xls[ori_num] == target_xls[tar_num]:
if target_xls[tar_num] == origin_xls[ori_num]:
flag = 'true'
break # 如果匹配到结果退出循环
if flag == 'true': # 匹配上结果输出后台日志
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 文件: ', tar_path+' '+ ' row:%d is ok' % (tar_num + 1))
success += 1
else: # 匹配不上将源表中行记录写入txt
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+ ' 文件: ', tar_path+' ' + ' row:%d is different' % (
tar_num + 1))
fail += 1
data = target_xls[tar_num]
#logstr = '文件: ', tar_path + ' ' + '【不一致】row<' + str(tar_num+1) + '>:' + str(data)
logstr = '【不一致】row<' + str(tar_num + 1) + '>:' + str(data)
#logstr1 = ' 文件: ', tar_path
#logstr = logstr2 + logstr1
writeappend_logfile(logname, logstr)
# logstr='【比对完成】总记录数:'+str(ori_num)+'条,一致:'+str(success)+'条,不一致:'+str(fail)+'条'
logstr = '【比对完成】总记录数:{:d}条,一致:{:d}条,不一致:{:d}条'.format(tar_num + 1, success, fail)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 【%s】比对结束' % sheet_tar.name)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 总记录数:%d条,一致:%d条,不一致:%d条' % (
tar_num + 1, success, fail))
writeappend_logfile(logname, logstr)
else:
errmsg='【'+sub_name+'】子表名不一致'
writeappend_logfile(logname,errmsg)
except Exception as err:
writeappend_logfile(logname,str(err)) #输出异常 def main():
pass if __name__ == '__main__': read_excel(r'1.xlsx','2.xlsx','Sheet1')

Python比较两个excel文档内容的异同的更多相关文章

  1. libreoffice python 操作word及excel文档

    1.开始.关闭libreoffice服务: 开始之前同步字体文件时间,是因为创建soffice服务时,服务会检查所需加载的文件的时间,如果其认为时间不符,则其可能会重新加载,耗时较长,因此需事先统一时 ...

  2. 使用NOPI读取Word、Excel文档内容

    使用NOPI读取Excel的例子很多,读取Word的例子不多. Excel的解析方式有多中,可以使用ODBC查询,把Excel作为一个数据集对待.也可以使用文档结构模型的方式进行解析,即解析Workb ...

  3. php读取excel文档内容(转载)

    入到数据库的需要,php-excel-reader可以很轻松的使用它读取excel文件,本文将详细介绍,需要了解的朋友可以参考下   php开发中肯定会遇到将excel文件内容导入到数据库的需要,ph ...

  4. 怎么对比两个excel文档的数据差异

    百度经验: https://jingyan.baidu.com/article/6181c3e0877c7a152ef15304.html

  5. Python openpyxl : Excel 文档简单操作

    安装方法 使用 pip 或通过专门python IDE(如pyCharm)进行安装 其中pip安装方法,命令行输入:  pip install openpyxl 基本使用 第一步先是要导入 openp ...

  6. 1、关于python第三方工具操作xls和xlsx格式的excel文档选型的吐血经历

    首先,最近看了python的一本书,其中第7章是关于文章操作的,就计划把python操作excel,word,txt,xml,html,json等格式的文档做个总结,并实现一些功能,但是,第一步就要把 ...

  7. 用Python操作excel文档

    使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...

  8. 使用Python操作Excel文档(一)

    Python | 使用Python操作Excel文档(一) 0 前言 在阅读本文之前,请确保您已满足或可能满足以下条件: 请确保您具备基本的Python编程能力. 请确保您会使用Excel. 请确保您 ...

  9. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

随机推荐

  1. 微信小程序、SSL证书、开启服务器TSL1.0、TSL1.1、TSL1.2服务

    微信小程序.SSL证书.开启服务器TSL1.0.TSL1.1.TSL1.2服务 https://blog.csdn.net/qq_32933615/article/details/70143105

  2. vsphere client 虚拟机随主机自动启动

  3. centos 6.x 安装配置 node.js 环境

    下载 可以在本地下载node.js最新版,然后通过ftp工具上传到服务器,或者直接在服务器终端使用wget命令下载(我当时下载的是node-v6.11.3-linux-x64版本,其他版本请查看上面链 ...

  4. p2p传输协议

    老司机是如何飙车的——P2P传输协议 转载来自2017-03-27 15:23 点波蓝字关注变智者 秋明山上人行稀,常有车手较高低,如今车道依旧在,不见当年老司机.其实老司机们从未离去,只不过好的车手 ...

  5. Delphi XE2 之 FireMonkey 入门(11) - 控件居中、旋转、透明

    RotationAngle.RotationCenter.Opacity 属性继承自 TControl(FMX.Types), 这些新属性成了控件的基本功能. 先在 HD 窗体上添加 TRectang ...

  6. R语言CSV文件

    R语言CSV文件 在R语言中,我们可以从存储在R环境外部的文件读取数据.还可以将数据写入由操作系统存储和访问的文件. R可以读取和写入各种文件格式,如:csv,excel,xml等. 在本章中,我们将 ...

  7. React-onsenui之RouterNavigator组件解读

    var index = 1;// index的最外层初始值,亦是全局 var MyPage = React.createClass({ //构成工具栏组件,根据hasBackButton的值为back ...

  8. Nginx配置之rewrite、proxy_pass、upstream、location

    如图,这是Nginx的配置文件nginx.conf中的一段配置代码. 在http段中定义了一个名为webservers的upstream模块,主要用于负载均衡. 在server模块中,定义了一个loc ...

  9. 东软、天健、金仕达、杭创、中联、NECHIS、军字一号 HIS产品的比较

    HIS主流厂商产品介绍1.沈阳东软医疗系统有限公司创立于1998年公司是中国目前最大的软件集团——东软集团为技术和资源依托,以研制生产大型医疗设备为主,同时为医院数字化提供全面解决方案.但是,公司改制 ...

  10. Airbnb开源 快速搭建企业级BI数据平台

    最近在公司做一个数据可视化相关的项目,使用了Airbnb开源维护的企业级BI数据平台superset,相较于tableau这种收费的商业软件,Superset是开源维护的,同时图表的种类和颜值普遍偏高 ...