# -*- 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. POJ 3661 DP

    题意: 思路: i表示到了i,j表示疲劳度为j f[i][j]表示能跑的最大距离 f[i][j]=f[i-1][j-1]+a[i] if(i-j>=0)f[i][0]=max(f[i][0],f ...

  2. .net 操作INI文件

    using System.Runtime.InteropServices; using System.Text; namespace FaureciaManager { public class Fi ...

  3. spinlock参考资料

    spinlock:http://irl.cs.ucla.edu/~yingdi/web/paperreading/smp_locking.pdf

  4. exsi主机之间使用scp拷贝文件超时问题

    exsi主机之间使用scp拷贝文件直接连接不上报错超时: 解决: 防火墙勾选ssh选项

  5. 编译php并与nginx整合

    告诉 Nginx 如何处理 php 文件:          nginx>vim  conf/nginx.conf                     location ~ \.php${ ...

  6. 学习Go语言之使用channel避免竞态问题

    // 使用channel避免竞态问题 package main import ( "fmt" "sync" ) var ( i int wg sync.Wait ...

  7. [POI2010]GIL-Guilds(结论题)

    题意 给一张无向图,要求你用黑白灰给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白点,至少有一个黑点与他相邻,对于任意一个灰点,至少同时有一个黑点和白点和灰点与他相邻,问能否成 ...

  8. mysql-5.7.25安装及常用语法

    我下的是免安装版的压缩文件包,可以选择下载.msi的程序包,那样就可以通过常见的图形界面来进行安装配置了 参考链接:https://blog.csdn.net/qq_23994787/article/ ...

  9. 今日SGU 5.30

    SGU 190 题意:给你个n*n的矩形,然后上面有几个点不能放东西,然后问你能不能用1*2的矩形,把能放 东西的地方放满 收获:一开始想的是,dfs,然后感觉这样的话,代码很长,而且很容易超时, 看 ...

  10. 05-数据类型转换(bool类型)