#-*- 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. Netflow elasticflow

    http://itfish.net/article/27660.html https://github.com/robcowart/elastiflow/tree/master

  2. 消息队列之 ActiveMQ

    简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效.可扩展.稳定.安全的企业级消息通信. 它的设计目标是提供标准的.面向消息的.多语言 ...

  3. C\C++语言中的宏多重展开和递归展开

    宏定义中的#,## 1. 宏中的参数前面使用一个#,预处理器会把这个参数转换为一个字符数组     2.记号粘贴操作符(token paste operator): ##            “## ...

  4. Denali NAND FLASH控制器的验证

    NAND FLASH的结构如图所示: Denali NAND FLASH控制器模块提供了从AHB总线到外部NAND FLASH存储器芯片IO管脚的访问功能.主要技术特性包括: 1.标准32位AHB总线 ...

  5. 职位-CHO:CHO

    ylbtech-职位-CHO:CHO 首席人力资源官(Chief Human Resource Officer),简称CHO.是具有制定集团化经营的公司人力资源的战略规划,并监督执行,负责建立畅通的沟 ...

  6. java 操作hdfs(连接HDFS)

    FileSystem fs = null; Configuration conf = null; @Before public void init() throws Exception{ conf = ...

  7. VMware vSphere 虚拟化简介

    目录 目录 vSphere 简介 vSphere 提供的工具 vCenter vCenter 的功能 vCenter 管理界面上提供的操作功能 HOST CLUSTER TEMPLATE Virtua ...

  8. vue中淡入淡出示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 《计算机程式设计》Week4 课堂笔记

    本笔记记录自 Coursera课程 <计算机程式设计> 台湾大学 刘邦锋老师 Week4 Functions 4-1 System Function 函数主要分为两大类系统定义函数与使用者 ...

  10. python 操作openpyxl导出Excel 设置单元格格式以及合并处理

    贴上一个例子,里面设计很多用法,根据将相同日期的某些行合并处理. from openpyxl import Workbook from openpyxl.styles import Font, Fil ...