# -*- 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. 洛谷P1137 旅行计划 解题报告(拓扑排序+DP)

    我看了一下其他大佬的题解,大部分都是拓扑排序加上DP.那么我想有的人是不明白为什么这么做的,拓扑排序有什么性质使得可以DP呢?下面我就提一下. 对一个有向无环图(Directed Acyclic Gr ...

  2. Laravel-路由组和中间件

    Laravel-路由组和中间件 标签(空格分隔): php 定义路由组 Route::group(['prefix'=>'Anime'], function(){ Rout::match(['g ...

  3. 关于Mantle使用个人的一些见解

    前一个月,我接触到了Mantle,由于项目采用的是MVC的设计模式,选用好的model也是至关重要的.先介绍下Mantle的使用吧. 首先定义好数据模型: @property (nonatomic, ...

  4. appium使用教程(三)-------------用例编写

    1. 驱动 import os, time, unittest from appium import webdriver PATH = lambda p:os.path.abspath(os.path ...

  5. MySQL 大数据量文本插入

    导入几万条数据需要等好几分钟的朋友来围观一下! 百万条数据插入,只在一瞬间.呵呵夸张,夸张!! 不到半分钟是真的! 插入指令: load data infile 'c:/wamp/tmp/Data_O ...

  6. JS文字特效:彩色滚动变幻效果,只适合少量的文字。(过多对页面有影响)

    JS代码如下: 代码具体是在哪里的我不知道但是我的有道云上有.如有哪位朋友知道,还望联系下,添加出处. <div id="chakhsu"></div> & ...

  7. POJ-2393 Yogurt factory 贪心问题

    题目链接:https://cn.vjudge.net/problem/POJ-2393 题意 有一个生产酸奶的工厂,还有一个酸奶放在其中不会坏的储存室 每一单元酸奶存放价格为每周s元,在接下来的N周时 ...

  8. 紫书 例题 10-20 UVa 10900(连续概率)

    分两类,当前第i题答或不答 如果不回答的话最大期望奖金为2的i次方 如果回答的话等于p* 下一道题的最大期望奖金 那么显然我们要取最大值 所以就要分类讨论 我们设答对i题后的最大期望奖金为d[i] 显 ...

  9. 题解 P2610 【[ZJOI2012]旅游】

    今天模拟赛考了这道题,那就来水一篇题解吧...(话说提高组模拟赛考什么省选题啊??) 这道题要我们求一条线段最多能经过的三角形数量. 回想小学学过的奥数,老师告诉过我们这样一件事:`点无大小 线无粗细 ...

  10. ECNUOJ 2150 完美的拯救

    完美的拯救 Time Limit:1000MS Memory Limit:65536KBTotal Submit:147 Accepted:50 Description  一只可怜的蚂蚁被万恶的魔术师 ...