Python比较两个excel文档内容的异同
- #-*- 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文档内容的异同的更多相关文章
- libreoffice python 操作word及excel文档
1.开始.关闭libreoffice服务: 开始之前同步字体文件时间,是因为创建soffice服务时,服务会检查所需加载的文件的时间,如果其认为时间不符,则其可能会重新加载,耗时较长,因此需事先统一时 ...
- 使用NOPI读取Word、Excel文档内容
使用NOPI读取Excel的例子很多,读取Word的例子不多. Excel的解析方式有多中,可以使用ODBC查询,把Excel作为一个数据集对待.也可以使用文档结构模型的方式进行解析,即解析Workb ...
- php读取excel文档内容(转载)
入到数据库的需要,php-excel-reader可以很轻松的使用它读取excel文件,本文将详细介绍,需要了解的朋友可以参考下 php开发中肯定会遇到将excel文件内容导入到数据库的需要,ph ...
- 怎么对比两个excel文档的数据差异
百度经验: https://jingyan.baidu.com/article/6181c3e0877c7a152ef15304.html
- Python openpyxl : Excel 文档简单操作
安装方法 使用 pip 或通过专门python IDE(如pyCharm)进行安装 其中pip安装方法,命令行输入: pip install openpyxl 基本使用 第一步先是要导入 openp ...
- 1、关于python第三方工具操作xls和xlsx格式的excel文档选型的吐血经历
首先,最近看了python的一本书,其中第7章是关于文章操作的,就计划把python操作excel,word,txt,xml,html,json等格式的文档做个总结,并实现一些功能,但是,第一步就要把 ...
- 用Python操作excel文档
使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...
- 使用Python操作Excel文档(一)
Python | 使用Python操作Excel文档(一) 0 前言 在阅读本文之前,请确保您已满足或可能满足以下条件: 请确保您具备基本的Python编程能力. 请确保您会使用Excel. 请确保您 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
随机推荐
- ODB Examples
http://www.codesynthesis.com/products/odb/examples.xhtml The following list gives an overview of the ...
- Python 代码控制Windows定时关机
为了在规定时间内实现电脑关机,我使用python编写了几行代码,最简单的实现了关机操作,后续再进行其它功能的添加(操作页面,取消等) import os,time #获取命令行输入的关机时间 inpu ...
- VMware 虚拟化编程(10) — VMware 数据块修改跟踪技术 CBT
目录 目录 前文列表 数据块修改跟踪技术 CBT 为虚拟机开启 CBT CBT 修改数据块偏移量获取函数 QueryChangedDiskAreas changeId 一个 QueryChangedD ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_6_LinkedHashSet集合
把www挪到最上面,第一个加入到哈希
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_6_File类判断功能的方法
exists true表示路径是存在的 不存在的路径 不存在的路径返回false 相对路径的文件的判断 ‘ 不存在的相对路径 isDirectory和isFile 路径是不存在的 用这个方法之前最好先 ...
- sklearn版本
10.19.0以前的sklearn版本才有cross_validation包,这个时候不要用model_selection导入StratifiedKFold,要用cross_validation,0. ...
- css 实现渐变
background:-ms-radial-gradient(circle,rgba(0, 0, 0, 0.2),rgba(0,0,0,0)); /* IE 10*/ background: -web ...
- uni-app-v-else中不需要值
这个问题把我都高懵逼了 在vue中, 但是在uni-app中:v-else不需要值, 下面去掉值就Ok了
- 【ABAP系列】SAP ABAP ALV里日期类型的F4帮助
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP ALV里日期类 ...
- 应用安全 - 渗透测试 - .net网站
注入 注入 单引号检测 - 多数使用MSSQL数据库 常规注入绕过 "or''=' | 'or''=' 'or'='or' | 'or'='or'" 上传 加图片头GIF89A