Python 实现Excel自动化办公(中)

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

复制代码
#统计员工男女比例def get_sex_percent():    sexlist=[]    for rows in range(table.nrows-1):        sexlist.append(table.cell(rows+1,2).value)    print(sexlist)    print("the 男女比 is:",round(sexlist.count("男")/sexlist.count("女"),2))​​​​​​

输出结果为:['男', '女', '男', '男', '男', '男', '男', '女', '女', '男', '男', '女', '男', '男', '男', '女', '男', '女', '男', '男']the 男女比 is: 2.33

#时间数据类型的处理def get_date():    for rows in range(table.nrows):        for cols in range(table.ncols):            if(table.cell(rows,cols).ctype==3):                date=xlrd.xldate_as_datetime(table.cell(rows,cols).value,0)                print(date)​​​​​​

#最后一列数据统计处理def get_statics():   sum=0   list1=[]   for rows in range(table.nrows-1):       sum+=int(table.cell(rows+1,table.ncols-1).value)       list1.append(int(table.cell(rows+1,table.ncols-1).value))       # print(table.cell(rows+1,8).value)       print("the sum is:",sum) #求和       print("the avg is:", round(sum /table.ncols, 2)) #取平均值       print("the max is:",sorted(list1)[-1]) #取最大值       print("the min is:",sorted(list1)[0]) #取最小值       if(len(list1)%2==0): #判断列表长度是奇还是偶,来取中位数           print("the median is:",(list1[int(len(list1)/2)]+list1[int(len(list1)/2+1)])/2)       else:           print("the median is:",list1[int((len(list1)+1)/2)])

if __name__ == '__main__': get_date() get_statics()
相关推荐
想进字节冲啊冲几秒前
Vibe Coding 实战指南:从“手写代码”到“意图设计”的前端范式转移
前端·ai编程
山卡拉噶6 分钟前
在Linux中安装Kdump调试环境
linux·运维·服务器
chxii23 分钟前
mybatis-spring 浅析
java·spring·mybatis
离&染31 分钟前
宝塔nginx一个域名部署两个前端和两个后端(VUE3)
前端·nginx
梅梅绵绵冰33 分钟前
springmvc文件上传
java·开发语言
龙华34 分钟前
Maven多仓库/依赖配置
java·maven
TT哇35 分钟前
【计算机网络】经典易错题 1.概述 2 物理层 3.数据链路层 4.网络层
android·服务器·计算机网络
天道佩恩35 分钟前
MapStruct转换实体
java·后端
朱哈哈O_o36 分钟前
前端通用包的作用——md5篇
前端
Boop_wu37 分钟前
[Java 面试] 多线程1
java·开发语言