python将考勤表中同一个人,同一日期的多行出勤时间转换成同一列显示的方法
原始表:
注意原始表中的日期和时间转换成文本的形式存储在EXCEL 中。
python代码:
import numpy as np
import xlrd
import datetime
import time
import numpy as np
import pandas as pd
fp=‘D:/乔雪梅/考勤表.xlsx’#原考勤表的存储路径
worksheet = xlrd.open_workbook(fp)
sheet_names= worksheet.sheet_names()
sheet = worksheet.sheet_by_name(sheet_names[0])
rows = sheet.nrows # 获取行数
#读取关键信息,包括人名,日期,时间
name=[]# 人名
dates=[] #日期
times=[]# 时间
#注意原始表格中的日期和时间在原始表格中转换为文本的方式存储,转换方式:=TEXT(E13,“hh:mm:ss”)
for i in range(1,rows) :
#cell = sheet.cell_value(i,0) # 注意0表示第1列数据,1表示第二列数据
name.append(sheet.cell_value(i,0))
dates.append(sheet.cell_value(i,1))
times.append(sheet.cell_value(i,2))
count=[]
colmn=[‘姓名’,‘考勤日期’,‘签到时间1’,‘签到时间2’,‘签到时间3’,‘签到时间4’,‘签到时间5’,‘签到时间6’,‘签到时间7’,‘签到时间8’]
count.append(colmn)
temp=[]
temp.append(name[0])
temp.append(dates[0])
for i in range(len(name)):
if name[i]==temp[0] and dates[i]==temp[1]:
temp.append(times[i])
else:
count.append(temp)
temp=[]
temp.append(name[i])
temp.append(dates[i])
print(name[i])
print(dates[i])
temp.append(times[i])
count.append(temp)
#将数据写入EXCEL表格的方法
data = pd.DataFrame(count)
writer = pd.ExcelWriter(‘A.xlsx’) # 写入Excel文件,路径可以任意指定
data.to_excel(writer, ‘page_1’, float_format=’%.5f’) # ‘page_1’是写入excel的sheet名
writer.save()
writer.close()
转换后的数据存储在与python文件同路径的以A命名的EXCEL表格中,转换后如下。