刚成立的公司,还没弄oa什么的,每月工时统计计算都需要人工去计算,写个工具先顶顶吧
用法很简单就是在脚本执行路径下放一个.xls 文件,脚本会自动去读,也就是下边变量filename的位置,名字不一样需要修改一下,文件是按姓名,月工作日时间,正常上班时间,异常时间,当然是根据我们公司写的,有其他需要各位自己改吧
脚本如下,各位雅正哈哈哈哈
[Python] 纯文本查看 复制代码 import xlrd
import xlutils.copy
lst = []
#取两位数字且不四舍五入
def get_two_float(f_str, n):
f_str = str(f_str) # f_str = '{}'.format(f_str) 也可以转换为字符串
a, b, c = f_str.partition('.')
c = (c + "0" * n)[:n] # 如论传入的函数有几位小数,在字符串后面都添加n为小数0
return ".".join([a, c])
#工时计算用的
def normal_staff(should, normal, wrong, name):
effective = float(normal) + float(wrong)
if effective == float(should):
lst.append(1)
elif effective < float(should):
small = float(effective) / 22.5
lst.append(get_two_float(small, 2))
elif effective > int(should):
bigger = (effective - float(should)) / 22.5 + 1
lst.append(get_two_float(bigger, 2))
#excel操作
def read_excel(filename):
wb = xlrd.open_workbook(filename) # 打开表格
sheet_name = wb.sheets()[0] # 选择sheet
return sheet_name
#统计excel行数
def excel_num(filename):
num = read_excel(filename)
row = num.nrows # 统计行数
return row
#修改excel文件内容
def excel_append(filename):
data = xlrd.open_workbook(filename)
sheet = data.sheets()[0]
ws = xlutils.copy.copy(data)
table = ws.get_sheet(0)
i = 0
for a in lst:
table.write(i, 4, a)
i += 1
ws.save('abc.xls')
if __name__ == '__main__':
filename = 'abc.xls'
re = read_excel(filename)
en = excel_num(filename)
for x in range(en):
name, should, normal, wrong = re.row_values(x)
normal_staff(should, normal, wrong, name)
excel_append(filename)
print('工时统计已完成')
|