# -*- coding: utf-8 -*-
import xlrd
import xlwt
import sys
from xlwt import *
from xlrd import open_workbook from xlrd import open_workbook
import sys #输出整个Excel文件的内容
def print_workbook(wb):
for s in wb.sheets():
print("Sheet:", s.name)
for r in range(s.nrows):
strRow = ""
for c in s.row(r):
#strRow += ("\t" + string(c.value))
print(c.value)
#print("ROW[" + r + "]:", strRow) #把一行转化为一个字符串
def row_to_str(row):
strRow = ""
for c in row:
strRow += ("\t" + c.value)
return strRow; #打印diff结果报表
def print_report(report):
for o in report:
if isinstance(o, list):
for i in o:
print("\t" + i)
else:
print (o) #diff两个Sheet
def diff_sheet(sheet1, sheet2):
nr1 = sheet1.nrows
nr2 = sheet2.nrows
nr = max(nr1, nr2)
report = []
for r in range(nr):
row1 = None;
row2 = None;
if r<nr1:
row1 = sheet1.row(r)
if r<nr2:
row2 = sheet2.row(r) diff = 0; # 0:equal, 1: not equal, 2: row2 is more, 3: row2 is less
if row1==None and row2!=None:
diff = 2
report.append("+ROW[" + str(r+1) + "]: " + row_to_str(row2))
if row1==None and row2==None:
diff = 0
if row1!=None and row2==None:
diff = 3
report.append("-ROW[" + str(r+1) + "]: " + row_to_str(row1))
if row1!=None and row2!=None:
# diff the two rows
reportRow = diff_row(row1, row2)
if len(reportRow)>0:
report.append("#ROW[" + str(r+1) + "]1: " + row_to_str(row1))
report.append("#ROW[" + str(r+1) + "]2: " + row_to_str(row2))
report.append(reportRow) return report; #diff两行
def diff_row(row1, row2):
nc1 = len(row1)
nc2 = len(row2)
nc = max(nc1, nc2)
report = []
for c in range(nc):
ce1 = None;
ce2 = None;
if c<nc1:
ce1 = row1[c]
if c<nc2:
ce2 = row2[c] diff = 0; # 0:equal, 1: not equal, 2: row2 is more, 3: row2 is less
if ce1==None and ce2!=None:
diff = 2
report.append("+CELL[" + str(c+1) + ": " + ce2.value)
if ce1==None and ce2==None:
diff = 0
if ce1!=None and ce2==None:
diff = 3
report.append("-CELL[" + str(c+1) + ": " + ce1.value)
if ce1!=None and ce2!=None:
if ce1.value == ce2.value:
diff = 0
else:
diff = 1
report.append("#CELL[" + str(c+1) + "]1: " + ce1.value)
report.append("#CELL[" + str(c+1) + "]2: " + ce2.value) return report '''if __name__=='__main__':
if len(sys.argv)<3:
exit() file1 = sys.argv[1]
file2 = sys.argv[2] wb1 = open_workbook(file1)
wb2 = open_workbook(file2) #print_workbook(wb1)
#print_workbook(wb2) #diff两个文件的第一个sheet
report = diff_sheet(wb1.sheet_by_index(0), wb2.sheet_by_index(0))
print file1 + "\n" + file2 + "\n#############################"
#打印diff结果
print_report(report)
'''
#对比两个表格差异
#打开一个xls文件,读取数据
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file,encoding_override='utf-8')
return data
except Exception as e:
print(e)
''' 得到一个excel的sheet个数
rb: 已经打开的excel对象
'''
def xl_sheet_num(rb):
count = len(b.sheets()) #sheet数量
return count ''' 获得一个excel所有的sheet名字
rb: 已经打开的excel对象
'''
def xl_sheet_name(rb):
count = len(rb.sheets())
for sheet in rb.sheets():
print(sheet.name)#sheet名称 '''获得表格中某个sheet某行的数据
file:Excel文件路径
colnameindex:行号
by_index:sheet 号
'''
def excel_table_byindex(data,by_index=0,rowindex=0):
#通过索引顺序获取一个表
table = data.sheets()[by_index]
nrows = table.nrows #行数
ncols = table.ncols #列数
print(nrows,ncols)
if rowindex in range(1,nrows):
#行列数据
row = table.row_values(rowindex)
print("row===",row)
app = {}
print("row_length==",len(row))
return row
else:
return null
'''对比两个表格的差异
'''
def excel_table_compare(rb_hw,rb_hq):
hw_sheet_num = xl_sheet_name(rb_hw)
hq_sheet_num = xl_sheet_name(rb_hq)
for i in range(hw_sheet_num):
table = rb_hw.sheets()[i]
nrows = table.nrows
ncols = table.ncols
for j in range(nrows):
com_string(rb_hw,rb_hq,)
'''
匹配关键字
compare:需要对比的excel
com_sheet:需要对比的sheet
com_row_index:需要对比的行
source:参考文件
sour_sheet:参考sheet
sour_row_index:参考文件行 '''
def com_string_row_col(compare,source,
com_sheet=0,com_row_index=0,sour_sheet=0,
sour_row_index=0):
com_row = excel_table_byindex(compare,com_sheet,com_row_index)
sour_row = excel_table_byindex(source,sour_sheet,sour_row_index)
for i in range(2,len(com_row)-2):
com_string = com_row[i];
if (com_string==""):
continue
print("com_string==",com_string)
if com_string in sour_row:
return 1
else:
return 0
''' '''
def com_string_row_sheet(rb_hw,rb_hq,hw_sheet,hw_row,hq_sheet):
hq_table = data.sheets()[by_index]
nrows = table.nrows
for i in range(nrows):
if (com_string_row_col(rb_hw,rb_hq,hw_sheet,hw_row,hq_sheet,i) ==1):
return 1
return 0 def main():
hw_File = "C:\\Users\\zwx318792\\Desktop\\xls_test\\huawei.xls"
hq_File = "C:\\Users\\zwx318792\\Desktop\\xls_test_change.xls" rb_hw = open_excel(hw_File)
rb_hq = open_excel(hq_File) print_workbook(rb_hw)
wb = Workbook()
#list_row = excel_table_byindex(rb_hw,1108,1)
#print(list)
isinclude = com_string(rb_hw,rb_hw,5,87,3,67)
print(isinclude) if __name__=="__main__":
main()

操作excel脚本练习的更多相关文章

  1. Ruby操作Excel的方法与技巧大全

    测试工作中,批量的数据通常会放到excel表格中,测试输出的数据写回表格中,这样输入输出易于管理,同时清晰明了 使用ruby来操作excel文件首先需要在脚本里包含以下语句 require'win32 ...

  2. Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合

    今天,接到一个任务,要生成大约两百个excel文件,从2006年到2013年,每个月两个文件,这些文件中除了几个关于日期的单元格不同外,其他数据都相同,所以就想到可以用python写一个小脚本,自动生 ...

  3. Python openpyxl、pandas操作Excel方法简介与具体实例

    本篇重点讲解windows系统下 Python3.5中第三方excel操作库-openpyxl: 其实Python第三方库有很多可以操作Excel,如:xlrd,xlwt,xlwings甚至注明的数据 ...

  4. 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器

    1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...

  5. python 操作excel 的包 函数

    ###########sample 1 https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399 python操作excel主要 ...

  6. python - 操作excel表格

    说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...

  7. xlrd》操作excel 出现的问题:File "D:\python37\lib\site-packages\xlrd\formula.py", line 1150, in evaluate_name_formula assert len(tgtobj.stack) == 1

    xlrd>操作excel  出现的问题 报错如下: D:\python37\python.exe D:/testWang/waimai/tools/get_excelData.py*** for ...

  8. 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)

    很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...

  9. C#通过NPOI操作Excel

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...

随机推荐

  1. 微信小程序领取卡券

    微信小程序领取卡券 标签(空格分隔): php 开发前需要准备的工作 1 小程序和公众号要有绑定 2 小程序和该公众号要绑定到同一个开发平台下 [https://open.weixin.qq.com/ ...

  2. C语言基础-第一章

    1.常量 直接常量: int mm=100; float nn=100.01; 字符常量:   编译指令,#define 常量名 常量值   (预处理命令,预处理命令都#开头.成为宏命令)  关键字, ...

  3. Bayes++ Library入门学习之熟悉class-Importance_resampler

    接下来,需要介绍的是重要性重采样类Bayesian_filter::Improtance_resampler.该类实现了两种重采样方法[1][2],和其子类的继承关系图如下: 其中Standard_r ...

  4. WLAN STA/AP 并发

    WLAN STA/AP 并发 Android 9 引入了可让设备同时在 STA 和 AP 模式下运行的功能.对于支持双频并发 (DBS) 的设备,此功能让一些新功能得以实现,例如在用户想要启用热点 ( ...

  5. bzoj3252: 攻略 优先队列 并查集 贪心

    考场上自己yy出来的做法..... Code: #include<cstdio> #include<algorithm> #include<queue> #incl ...

  6. bzoj2763 [JLOI]飞行路线 分层图最短路

    问题描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  7. iotop---监控磁盘I/O 使用状况

    iotop命令是一个用来监视磁盘I/O使用状况的top类工具.iotop具有与top相似的UI,其中包括PID.用户.I/O.进程等相关信息.Linux下的IO统计工具如iostat,nmon等大多数 ...

  8. scrapy xpath选择器多级选择错误

    在学习scrapy中用xpath提取网页内容时,有时要先提取出一整个行标签内容,再从行标签里寻找目标内容.出现一个错误. 错误代码: def parse(self, response): sel = ...

  9. OpenJDK源码研究笔记(九)-可恨却又可亲的的异常(NullPointerException)

    可恨的异常 程序开发过程中,最讨厌异常了. 异常代表着程序出了问题,一旦出现,控制台会出现一屏又一屏的堆栈错误信息. 看着就让人心烦. 对于一个新人来讲,遇到异常经常会压力大,手忙脚乱,心生畏惧. 可 ...

  10. 解析XML文件之使用DOM解析器

    在前面的文章中.介绍了使用SAX解析器对XML文件进行解析.SAX解析器的长处就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析. DOM解析器的长处可能是理解起来比較的直观,当然, ...