• python: excel假期时间提取统计


    1. # encoding: utf-8
    2. # 版权所有 2023 涂聚文有限公司
    3. # 许可信息查看:
    4. # 描述:
    5. # Author : geovindu,Geovin Du 涂聚文.
    6. # IDE : PyCharm 2023.1 python 311
    7. # Datetime : 2023/9/3 7:04
    8. # User : geovindu
    9. # Product : PyCharm
    10. # Project : LukfookLeaveCalculation
    11. # File : EmpLoyeeHolidaysGet.py
    12. # explain : 学习
    13. import sys
    14. import os
    15. import Common.Utils
    16. import Model.Holiday
    17. class EmpLoyeeHolidaysGet(object):
    18. """
    19. 抽取需要的数据
    20. """
    21. def getYear(self,strsource):
    22. """
    23. :param strsource:
    24. :return:
    25. """
    26. refloat = 0
    27. if (strsource != None):
    28. if ("年" in strsource):
    29. refloat=Common.Utils.Utils.getYear(strsource)
    30. return refloat
    31. def getMonth(self, strsource):
    32. """
    33. :param strsource:
    34. :return:
    35. """
    36. refloat = 0
    37. if (strsource != None):
    38. if ("月" in strsource):
    39. refloat = Common.Utils.Utils.getMonth(strsource)
    40. return refloat
    41. def getDay(self, strsource):
    42. """
    43. :param strsource:
    44. :return:
    45. """
    46. refloat = 0
    47. if (strsource != None):
    48. if ("日" in strsource):
    49. refloat = Common.Utils.Utils.getDay(strsource)
    50. return refloat
    51. def getHolidays(self,strsource):
    52. """
    53. 獲得假期限列表
    54. :param strsource:
    55. :return:
    56. """
    57. hday=[]
    58. if (strsource != None):
    59. if("星期" not in strsource):
    60. if("nan" not in strsource):
    61. spstr = strsource.split(' ') # 有两个,有换行符,两个假期,一定要这换行符 NaN
    62. if (len(spstr) > 1):
    63. for i in range(len(spstr)):
    64. sk = spstr[i]
    65. #print("for sk", sk)
    66. hd = Model.Holiday.Holiday()
    67. hd=EmpLoyeeHolidaysGet.checkHoliday(self,sk)
    68. hday.append(hd)
    69. elif (len(spstr) == 1):
    70. sk = spstr[0]
    71. #print("sk",sk)
    72. hd = Model.Holiday.Holiday()
    73. hd = EmpLoyeeHolidaysGet.checkHoliday(self,sk)
    74. hday.append(hd)
    75. else:
    76. hday = []
    77. else:
    78. hday = []
    79. return hday
    80. def checkHoliday(self,strsource):
    81. """
    82. 检测是什么假期
    83. :param strsource:
    84. :return:
    85. """
    86. hd=Model.Holiday.Holiday()
    87. #print("chek")
    88. if ("例休" in strsource):
    89. #print("cehck例休")
    90. hd.HolidayId=1
    91. hd.HolidayName="例休"
    92. hd.WorkTime=Common.Utils.Utils.getRegularHoliday(strsource)
    93. if ("特假" in strsource):
    94. hd.HolidayId=2
    95. hd.HolidayName="特假"
    96. hd.workTime=Common.Utils.Utils.getSpecialLeave(strsource)
    97. if ("補休" in strsource):
    98. hd.HolidayId=3
    99. hd.HolidayName="補休"
    100. hd.workTime = Common.Utils.Utils.getDeferredHoliday(strsource)
    101. if ("加班" in strsource):
    102. hd.HolidayId=4
    103. hd.HolidayName="加班"
    104. hd.workTime = Common.Utils.Utils.getOvertime(strsource)
    105. if ("事假" in strsource):
    106. hd.HolidayId=5
    107. hd.HolidayName="事假"
    108. hd.workTime = Common.Utils.Utils.getPersonalLeave(strsource)
    109. if ("年假" in strsource):
    110. hd.HolidayId = 6
    111. hd.HolidayName = "年假"
    112. hd.workTime = Common.Utils.Utils.getAnnualLeave(strsource)
    113. if ("病假" in strsource):
    114. hd.HolidayId = 7
    115. hd.HolidayName = "病假"
    116. hd.workTime = Common.Utils.Utils.getSickLeave(strsource)
    117. if ("育兒假" in strsource):
    118. hd.HolidayId = 8
    119. hd.HolidayName = "育兒假"
    120. hd.workTime = Common.Utils.Utils.getChildcareLeave(strsource)
    121. if ("遲到" in strsource):
    122. hd.HolidayId = 9
    123. hd.HolidayName = "遲到"
    124. hd.workTime = Common.Utils.Utils.getLateTime(strsource)
    125. return hd
    126. def getEmplee(self,Empleelist,id:int):
    127. """
    128. 從員工列表中找到對應ID員工的信息
    129. :param Empleelist:
    130. :param id:
    131. :return:
    132. """
    133. ee=[]
    134. for em in Empleelist:
    135. if(em.EmployeeId==id):
    136. ee.append(em.EmployeeNo)
    137. ee.append(em.EmployeeDep)
    138. ee.append(em.EmployeeName)
    139. return ee
    140. def getHoliday(self,holidays):
    141. """
    142. 返回假期
    143. :param holidays:
    144. :return:
    145. """
    146. getname=""
    147. if(len(holidays)>0):
    148. for hd in holidays:
    149. if(hd.HolidayId==1):
    150. getname=getname+hd.HolidayName+str(hd.WorkTime)+"天 "
    151. elif(hd.HolidayId==9):
    152. getname = getname + hd.HolidayName + str(hd.WorkTime) + "分钟 "
    153. else:
    154. getname = getname + hd.HolidayName + str(hd.WorkTime) + "小时 "
    155. return getname
    156. def getHolidayAnnualLeave(self,holidays):
    157. """
    158. 返回年假数
    159. :param holidays:
    160. :return:
    161. """
    162. getsum=0
    163. if(len(holidays)>0):
    164. for hd in holidays:
    165. if(hd.HolidayId==6):
    166. getsum=getsum+hd.WorkTime
    167. #print("sum:",getsum,hd.HolidayName)
    168. return getsum
    169. def getHolidayOvertime(self,holidays):
    170. """
    171. 返回加班数
    172. :param holidays:
    173. :return:
    174. """
    175. getsum=0
    176. if(len(holidays)>0):
    177. for hd in holidays:
    178. if(hd.HolidayId==4):
    179. getsum=getsum+hd.WorkTime
    180. #print("sum:",getsum,hd.HolidayName)
    181. return getsum
    182. def getHolidayDeferred(self,holidays):
    183. """
    184. 返回補休数
    185. :param holidays:
    186. :return:
    187. """
    188. getsum=0
    189. if(len(holidays)>0):
    190. for hd in holidays:
    191. if(hd.HolidayId==3):
    192. getsum=getsum+hd.WorkTime
    193. #print("sum:",getsum,hd.HolidayName)
    194. return getsum
    195. def getEmpleeIdAnnualLeave(self, id:int,listEmp:list):
    196. """
    197. 年假ID
    198. :param self:
    199. :param listEmp:
    200. :return:
    201. """
    202. annual=0
    203. #print(listEmp)
    204. for sd in listEmp:
    205. #print(sd[0],sd[1])
    206. if(sd[0]==id):
    207. #print(sd[1])
    208. annual=annual+sd[1]
    209. return annual
    210. def getEmpleeIdOvertime(self, id:int,listEmp:list):
    211. """
    212. 加班时间ID
    213. :param self:
    214. :param listEmp:
    215. :return:
    216. """
    217. annual=0
    218. #print(listEmp)
    219. for sd in listEmp:
    220. #print(sd[0],sd[1])
    221. if(sd[0]==id):
    222. #print(sd[1])
    223. annual=annual+sd[1]
    224. return annual
    225. def getEmpleeIdeferred(self, id:int,listEmp:list):
    226. """
    227. 调休时间ID
    228. :param self:
    229. :param listEmp:
    230. :return:
    231. """
    232. annual=0
    233. #print(listEmp)
    234. for sd in listEmp:
    235. #print(sd[0],sd[1])
    236. if(sd[0]==id):
    237. #print(sd[1])
    238. annual=annual+sd[1]
    239. return annual
    1. # This is a sample Python script.
    2. # encoding: utf-8
    3. # 版权所有 2023 涂聚文有限公司
    4. # 许可信息查看:
    5. # 描述:
    6. # Author : geovindu,Geovin Du 涂聚文.
    7. # IDE : PyCharm 2023.1 python 311
    8. # Datetime : 2023/9/2 20:20
    9. # User : geovindu
    10. # Product : PyCharm
    11. # Project : LukfookLeaveCalculation
    12. # File : main.py
    13. # explain : 学习
    14. # Press Shift+F10 to execute it or replace it with your code.
    15. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
    16. import xlrd
    17. import xlwt
    18. import xlwings as xw
    19. import xlsxwriter
    20. import openpyxl as openws
    21. import pandas as pd
    22. import numpy as np
    23. import pandasql
    24. import os
    25. import sys
    26. import Common.Utils
    27. import BLL.EmpLoyeeHolidaysGet
    28. import Model.Employee
    29. import Model.HolidayList
    30. from openpyxl import load_workbook
    31. def print_hi(name):
    32. # Use a breakpoint in the code line below to debug your script.
    33. print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
    34. # Press the green button in the gutter to run the script.
    35. if __name__ == '__main__':
    36. print_hi('PyCharm')
    37. unstr=Common.Utils.Utils()
    38. strtest="年假2小時 事假0.5小時" # 换行符
    39. #strtest-strtest.replace('_x000D_', ' ', regex=True)
    40. #strtest = "NaN"
    41. spstr = strtest.split(" ")
    42. thbll = BLL.EmpLoyeeHolidaysGet.EmpLoyeeHolidaysGet()
    43. tt=thbll.getHolidays(spstr[0])
    44. print("tt",tt[0].WorkTime,spstr)
    45. dataframe1 = pd.read_excel('2023年7月.xlsx')
    46. #print(dataframe1)
    47. #for idx, data in dataframe1.iterrows():
    48. #print("[{}]: {}".format(idx, data))
    49. # 第1列为日期,其他列是员工对应的列的考勤
    50. print("*************")
    51. print(dataframe1.index)
    52. print("*************")
    53. print(dataframe1.shape) #行列
    54. datarowcol=dataframe1.shape
    55. print("行:",datarowcol[0],",列:",datarowcol[1])
    56. excelRows=datarowcol[0]
    57. excelColumns=datarowcol[1]
    58. print("*********列标题****")
    59. print(dataframe1.columns) #第一行标题 pandas.core.indexes.base.Index
    60. titls=dataframe1.columns.to_list()
    61. print(titls)
    62. print("标题:",titls[1])
    63. year=thbll.getYear(titls[1])
    64. print("*************",year)
    65. dmps = []
    66. # 姓名,工号,序号,部门 0行至3行为员工资料 第一行为标题,不算行的内容 可以切片方式GET数据
    67. #序号
    68. idlist =[]
    69. dataId = dataframe1.loc[0:0]
    70. for idx, datadd in dataId.iterrows():
    71. print("[{}]: {}".format(idx, datadd))
    72. idlist=datadd.to_list()
    73. for idd in range(1,len(idlist)):
    74. #employvee=Model.Employee.employee()
    75. #employvee.EmployeeId=idd
    76. #dmps.append(employvee)
    77. print(idd)
    78. print("*******序号******")
    79. #工号
    80. nolist=[]
    81. dataNo = dataframe1.loc[1:1]
    82. for idx, dataoo in dataNo.iterrows():
    83. print("[{}]: {}".format(idx, dataoo))
    84. nolist=dataoo.to_list()
    85. print("*******工号******")
    86. #部门
    87. deplist=[]
    88. dataDep = dataframe1.loc[2:2]
    89. for idx, datapp in dataDep.iterrows():
    90. print("[{}]: {}".format(idx, datapp))
    91. deplist=datapp.to_list()
    92. print("*******部门******")
    93. #姓名
    94. namelist=[]
    95. dataName = dataframe1.loc[3:3]
    96. for idx, datann in dataName.iterrows():
    97. print("[{}]: {}".format(idx, datann))
    98. namelist=datann.to_list()
    99. print("*******姓名******")
    100. for i in range(1,len(namelist)):
    101. employvee = Model.Employee.employee()
    102. employvee.EmployeeId=idlist[i]
    103. employvee.EmployeeNo=nolist[i]
    104. employvee.EmployeeName=namelist[i]
    105. employvee.EmployeeDep=deplist[i]
    106. dmps.append(employvee)
    107. for ob in dmps:
    108. print(ob.EmployeeId,ob.EmployeeNo,ob.EmployeeName,ob.EmployeeDep)
    109. #data1 = dataframe1.loc[0:3]
    110. #print(type(data1))
    111. #print(data1)
    112. #for idx, data in data1.iterrows():
    113. #print("[{}]: {}".format(idx, data))
    114. #print("*************")
    115. AnnualLeave=0
    116. Overtime=0
    117. DeferredHoliday=0
    118. SpecialLeave=0
    119. PersonalLeave=0
    120. SickLeave=0
    121. ChildcareLeave=0
    122. LateTime=0
    123. RegularHoliday=0
    124. # 内容
    125. mon=0
    126. day=0
    127. getdate=[]
    128. getHolidays=[]
    129. readrows=(excelRows-1)-3 #索此值从零开始,所以总数减一,再减三行统计的
    130. data4 = dataframe1.loc[4:readrows] #第四行开始内空
    131. getrow=1
    132. emplist=[]
    133. for idx, datavalue in data4.iterrows():
    134. #strnum=Common.Utils.Utils.getAnnualLeave(data)
    135. employs = Model.Employee.employee()
    136. print("[{}]: {}".format(idx, datavalue))
    137. slist=datavalue.tolist()
    138. hbll=BLL.EmpLoyeeHolidaysGet.EmpLoyeeHolidaysGet() #病假4小時_x000D_事假0.5小時 未处理
    139. #print(slist) #这是读取第1列的日期数据
    140. for i in range(0, 1):
    141. strvalue = str(slist[i])
    142. print("get value:", strvalue.replace('nan', ''), type(strvalue))
    143. mon=hbll.getMonth(strvalue)
    144. day=hbll.getDay(strvalue)
    145. print("*******时间************",mon,day)
    146. getdate.append(str(year)+'-'+str(mon)+'-'+str(day))
    147. print(slist)
    148. hid=1
    149. for i in range(1,len(slist)):
    150. getdd = Model.HolidayList.HolidayList()
    151. getdd.HolidayDate = str(year) + '-' + str(mon) + '-' + str(day)
    152. #strvalue=str(slist[i]).replace(r'\s+|\\n', ' ', regex=True) _x000D_
    153. #isnan=np.isnan(slist[i])
    154. #if(isnan):
    155. strvalue = str(slist[i]).replace('_x000D_', ' ') # 规换单元格的换行符,否则处理不了正确数据
    156. #strvalue = str(slist[i]).replace('nan', ' ')
    157. #print("get value:",strvalue.replace('nan',''),type(strvalue))
    158. strnums = hbll.getHolidays(strvalue) # float str
    159. print("hid:",hid)
    160. getdd.HolidayEmpId = hid
    161. if(len(strnums)>0):
    162. for sn in range(len(strnums)):
    163. print("str:",strnums[sn].HolidayId,strnums[sn].HolidayName,strnums[sn].WorkTime)
    164. if strnums[sn].HolidayId==6:
    165. AnnualLeave=AnnualLeave+strnums[sn].WorkTime
    166. if strnums[sn].HolidayId == 4:
    167. Overtime=Overtime+strnums[sn].WorkTime
    168. if strnums[sn].HolidayId == 3:
    169. DeferredHoliday = DeferredHoliday + strnums[sn].WorkTime
    170. if strnums[sn].HolidayId == 2:
    171. SpecialLeave=SpecialLeave+strnums[sn].WorkTime
    172. if strnums[sn].HolidayId == 5:
    173. PersonalLeave=PersonalLeave+strnums[sn].WorkTime
    174. if strnums[sn].HolidayId == 7:
    175. SickLeave=SickLeave+strnums[sn].WorkTime
    176. if strnums[sn].HolidayId == 8:
    177. ChildcareLeave=ChildcareLeave+strnums[sn].WorkTime
    178. if strnums[sn].HolidayId == 9:
    179. LateTime=LateTime+strnums[sn].WorkTime
    180. if strnums[sn].HolidayId == 1:
    181. RegularHoliday=RegularHoliday+strnums[sn].WorkTime
    182. getdd.Holdays = strnums
    183. else:
    184. print("0")
    185. getdd.Holdays =[]
    186. hid+=1
    187. getHolidays.append((getdd))
    188. employs.EmployeeHolidays=getHolidays
    189. employs.EmployeeActiveDate=getdate
    190. getrow+=1
    191. # getHolidays.append(strnums)
    192. print("类型:", type(datavalue))
    193. emplist.append((employs))
    194. flist=[]
    195. getIdAnnualLeave=[]
    196. fdata=('ID','工号','部门','姓名','日期','假期')
    197. flist.append(fdata)
    198. print("date:",getdate,len(getdate))
    199. print(len(getHolidays))
    200. #for dddd in emplist:
    201. #print(dddd.EmployeeId,len(dddd.EmployeeHolidays)) #emplist[0].EmployeeHolidays:
    202. for sd in emplist[0].EmployeeHolidays:
    203. fdata=(sd.HolidayEmpId,thbll.getEmplee(dmps,sd.HolidayEmpId)[0],thbll.getEmplee(dmps,sd.HolidayEmpId)[1],thbll.getEmplee(dmps,sd.HolidayEmpId)[2],sd.HolidayDate,thbll.getHoliday(sd.Holdays))
    204. flist.append(fdata)
    205. #ananuaint=thbll.getEmpleeIdAnnualLeave(sd.HolidayEmpId,emplist[0].EmployeeHolidays)
    206. #getIdAnnualLeave.append(ananuaint)
    207. print(sd.HolidayEmpId,thbll.getEmplee(dmps,sd.HolidayEmpId)[0],thbll.getEmplee(dmps,sd.HolidayEmpId)[1],thbll.getEmplee(dmps,sd.HolidayEmpId)[2],sd.HolidayDate,thbll.getHoliday(sd.Holdays))
    208. print("**************")
    209. print(getIdAnnualLeave)
    210. print("*************",year,"年",mon,"月","考勤合计*************")#,day,"日"
    211. print("年假合计:",AnnualLeave,"小时,加班合计:",Overtime,"小时,补休合计:",DeferredHoliday,"小时,特假合计:",SpecialLeave,"小时")
    212. print("事假合计",PersonalLeave,"小时,病假合计:",SickLeave,"小时,迟到合计:",LateTime,"分钟,育儿假合计:",ChildcareLeave,"小时")
    213. print("例休:",RegularHoliday,"天")
    214. print("*****************************************************************")
    215. #年假
    216. for sd in emplist[0].EmployeeHolidays:
    217. ananuaint=[sd.HolidayEmpId,thbll.getHolidayAnnualLeave(sd.Holdays)]
    218. getIdAnnualLeave.append(ananuaint)
    219. print(getIdAnnualLeave)
    220. dd=thbll.getEmpleeIdAnnualLeave(1,getIdAnnualLeave)
    221. print("年假 id=1 sum:",dd)
    222. #加班
    223. getIDOvertimm=[]
    224. for sd in emplist[0].EmployeeHolidays:
    225. overtimeint=[sd.HolidayEmpId,thbll.getHolidayOvertime(sd.Holdays)]
    226. getIDOvertimm.append(overtimeint)
    227. print(getIDOvertimm)
    228. dov=thbll.getEmpleeIdOvertime(1,getIDOvertimm)
    229. print("加班 id=1 sum:",dov)
    230. #补休
    231. getIdDeferred=[]
    232. for sd in emplist[0].EmployeeHolidays:
    233. defint=[sd.HolidayEmpId,thbll.getHolidayDeferred(sd.Holdays)]
    234. getIdDeferred.append(defint)
    235. print(getIdDeferred)
    236. doef=thbll.getEmpleeIdeferred(1,getIdDeferred)
    237. print("补休 id=1 sum:",doef)
    238. '''
    239. '''
    240. taum= []
    241. tva=("名称","合计")
    242. taum.append(tva)
    243. tva=("年假", str(AnnualLeave) + "小时")
    244. taum.append(tva)
    245. tva=("加班",str(Overtime)+"小时")
    246. taum.append(tva)
    247. tva=("补休",str(DeferredHoliday)+"小时")
    248. taum.append(tva)
    249. tva=("特假",str(SpecialLeave)+"小时")
    250. taum.append(tva)
    251. tva=("事假",str(PersonalLeave)+"小时")
    252. taum.append(tva)
    253. tva=("病假",str(SickLeave)+"小时")
    254. taum.append(tva)
    255. tva=("迟到",str(LateTime)+"分钟")
    256. taum.append(tva)
    257. tva=("育儿假",str(ChildcareLeave)+"小时")
    258. taum.append(tva)
    259. tva=("例休",str(RegularHoliday)+"天")
    260. taum.append(tva)
    261. sheetname1 = str(year) + "年" + str(mon) + "月假期"
    262. writer = pd.ExcelWriter(sheetname1+"方案.xlsx") # 这里是创建了可写入不同sheet的文件
    263. text1 = pd.DataFrame(flist,columns=['ID','工号','部门','姓名','日期','假期'])
    264. text1.to_excel(writer, sheet_name=sheetname1,header=0, index=False) # sheet命名为
    265. sheetname2=str(year)+"年"+str(mon)+"月合计"
    266. text2 = pd.DataFrame(taum) #,columns=['名称','合计']
    267. text2.to_excel(writer, sheet_name=sheetname2, header=0, index=False) # sheet命名为
    268. text3=dataframe1;
    269. sheetname3=str(year) + "年" + str(mon) + "月明細"
    270. text3.to_excel(writer,sheet_name=sheetname3, header=0, index=False)
    271. #writer.sheets.update()
    272. writer.close()
    273. dataframe2 = openws.load_workbook("2023年7月.xlsx")
    274. # Define variable to read sheet
    275. sheet = dataframe2.active
    276. setrow=datarowcol[0] #总共行
    277. setcol=datarowcol[1] #总共列
    278. #每个人的合计
    279. for idd in range(1, len(idlist)):
    280. sheet.cell(row=setrow-1, column=1+idd).value = str(thbll.getEmpleeIdAnnualLeave(idd,getIdAnnualLeave)) + "小时"
    281. sheet.cell(row=setrow - 0, column=1+idd).value = str(thbll.getEmpleeIdOvertime(idd,getIDOvertimm))+"小时"
    282. sheet.cell(row=setrow +1, column=1+idd).value = str(thbll.getEmpleeIdeferred(idd,getIdDeferred))+"小时"
    283. #合部汇总
    284. sheet.cell(row=setrow+2, column=1).value = "年假汇总"
    285. sheet.cell(row=setrow +3, column=1).value = "加班汇总"
    286. sheet.cell(row=setrow +4, column=1).value = "补休汇总"
    287. sheet.cell(row=setrow+2, column=2).value = str(AnnualLeave) + "小时"
    288. sheet.cell(row=setrow +3, column=2).value = str(Overtime)+"小时"
    289. sheet.cell(row=setrow +4, column=2).value = str(DeferredHoliday)+"小时"
    290. dataframe2.save(r'demo.xlsx')
    291. # Iterate the loop to read the cell values
    292. #for row in range(0, dataframe2.max_row):
    293. #for col in dataframe2.iter_cols(1, dataframe2.max_column):
    294. #print(col[row].value)
    295. # a single cell
    296. #v1 = xw.range("A1:A7").value
    297. #v2 = xw.range("F5").value
    298. #print("Result:", v1)
    299. # https://www.geeksforgeeks.org/how-to-add-one-row-in-an-existing-pandas-dataframe/ 新添加一行
    300. # See PyCharm help at https://www.jetbrains.com/help/pycharm/

  • 相关阅读:
    算法-递推
    Go 原子操作有哪些?
    MYSQL语法一:创建表和查看表的所有列和所有数据库等
    python中如何打印日志信息
    关与一个SpringBoot单体项目的优化-->瑞吉外卖优化
    登陆认证&权限控制(1)——从session到token认证的变迁 & session的问题分析 + CSRF攻击的认识
    8. Java面向对象编程(三)
    【第六篇】- Maven 仓库
    8000字讲透OBSA原理与应用实践
    数据结构之队列
  • 原文地址:https://blog.csdn.net/geovindu/article/details/132893374