#-*- 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. ORACLE动态监听

    动态监听的原理 pmon在数据库启动到mount或open时,动态从参数文件中读取service_names值.service_names可以为多值(可以有64个,其中包括两个系统的).  servi ...

  2. 《图解设计模式》读书笔记8-3 STATE模式

    目录 State模式 示例程序 实现的功能 不使用&使用状态模式对比 示例程序的类图 代码 角色和类图 角色 类图 拓展思路 分而治之 依赖于状态的处理 谁来管理状态迁移 易于增加新状态 实例 ...

  3. Week 9 - 638.Shopping Offers - Medium

    638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...

  4. C#—Nhibernate使用教程

    本篇文章,让我们一起来探索Nhibernate.首先我们去搜索Nhibernate下载地址,如下链接所示.该版本可能是最新版,我下载的4.0.4.GA.其中GA意思我没搞清楚.不过应该不重要.http ...

  5. SpringBoot 使用maven创建springboot项目

    有两种方式可以创建  1是使用spring-boot-starter-parent ,2是使用spring-boot-dependencies (即父项目dependencyManagement) ( ...

  6. POJ-2352.Stats(树状数组简单应用)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 58255   Accepted: 24860 Descripti ...

  7. El 表达式和 Jstl 标签库

    El 表达式学习 1. 什么是 EL 表达式 全称:Expression Language,一种写法非常简介的表达式.语法简单易懂,便于使用.表达式语言的灵感来自于 ECMAScript 和XPath ...

  8. 【接口工具】mac环境下使用Charles抓包Https请求

    Charles支持针对Https包解析.具体安装导航请参考“[接口工具]接口抓包工具之Charles” 操作流程 电脑安装证书: Help-SSL Proxying-Install Charles R ...

  9. 一台电脑关联多个git账号

    一台电脑连接多个git账号 现需要一台电脑连接gitlab,github,码云,之前的操作时,用公司账号,在这几个地方都注册一遍,导致自己就有两类号,一个自己的,一个公司的,这样也是可以,但总是不太好 ...

  10. wxpython中列表框(ListBox类)、复选列表框(CheckListBox)、下拉选项(Choice)、进度条(Gauge)、滑块(Slider)使用实例源码分享

    #coding=utf-8 import wx import time class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(s ...