Python 实现Excel自动化办公《中》

欢迎关注【无量测试之道】公众号,回复【领取资源】,
Python编程学习资源干货、
Python+Appium框架APP的UI自动化、
Python+Selenium框架Web的UI自动化、
Python+Unittest框架API自动化、

资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。

在上一篇文章的基础上进行一些特殊的处理,这里的特殊处理主要是涉及到了日期格式数据的处理(上一篇文章大家估计也看到了日期数据的处理是不对的)以及常用的聚合数据统计处理,可以有效的实现你的常用统计要求。代码如下:

1 #统计员工男女比例
2 def get_sex_percent():
3     sexlist=[]
4     for rows in range(table.nrows-1):
5         sexlist.append(table.cell(rows+1,2).value)
6     print(sexlist)
7     print("the 男女比 is:",round(sexlist.count("")/sexlist.count(""),2))
1 输出结果为:
2 ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
3 the 男女比 is: 2.33
1 #时间数据类型的处理
2 def get_date():
3     for rows in range(table.nrows):
4         for cols in range(table.ncols):
5             if(table.cell(rows,cols).ctype==3):
6                 date=xlrd.xldate_as_datetime(table.cell(rows,cols).value,0)
7                 print(date)
 1 #最后一列数据统计处理
 2 def get_statics():
 3    sum=0
 4    list1=[]
 5    for rows in range(table.nrows-1):
 6        sum+=int(table.cell(rows+1,table.ncols-1).value)
 7        list1.append(int(table.cell(rows+1,table.ncols-1).value))
 8        # print(table.cell(rows+1,8).value)
 9        print("the sum is:",sum) #求和
10        print("the avg is:", round(sum /table.ncols, 2)) #取平均值
11        print("the max is:",sorted(list1)[-1]) #取最大值
12        print("the min is:",sorted(list1)[0]) #取最小值
13        if(len(list1)%2==0): #判断列表长度是奇还是偶,来取中位数
14            print("the median is:",(list1[int(len(list1)/2)]+list1[int(len(list1)/2+1)])/2)
15        else:
16            print("the median is:",list1[int((len(list1)+1)/2)])
17  
18  
19 if __name__ == '__main__':
20  get_date()
21  get_statics()

以上两个方法代码执行效果如下:

备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:

 添加关注,让我们一起共同成长!

原文地址:https://www.cnblogs.com/Wu13241454771/p/13572735.html