标签:python 中控考勤机
用python连接中控考勤机。 下载并分析数据,把结果邮件给人事。
SDK包建议用32位的,在win7 64位系统上用64位开发包不行,用32可以。
python还要pywin32 注意版本,我这用的 32位的python 2.7 然后下的这个pywin32
#!/usr/bin/env python #_*_ coding:gbk _*_ import win32com.client import time import sys import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage def write_file(filename, data): with open(filename, ‘w‘) as f: f.write(data) def send_mail(filename=[], picname=[], content_txt=‘‘, content_html=‘‘): smtpserver = ‘smtp.163.com‘ username = ‘test@163.com‘ password = ‘abc123‘ msg = MIMEMultipart() msg[‘Subject‘] = ‘Check_In‘ msg[‘From‘] = "test@163.com" msg[‘To‘] = "rev@163.com" # attchment if len(filename) > 0: for i in filename: att = MIMEText(open(i, ‘rb‘).read(), ‘base64‘, ‘gf2312‘) att["Content-Type"] = ‘application/octet-stream‘ att["Content-Disposition"] = ‘attachment; filename="%s"‘ % i.split(‘\\‘)[-1] msg.attach(att) # attchment picture if len(picname) > 0 and content_html != ‘‘: for i in range(0,len(picname)): #content_html = ‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image%s"><br>good!‘ % i msg_content_html = MIMEText(content_html,‘html‘,‘gb2312‘) msg.attach(msg_content_html) with open(picname[i], ‘rb‘) as f: msgImage = MIMEImage(f.read()) msgImage.add_header(‘Content-ID‘, ‘<image%s>‘ % (i + 1)) msg.attach(msgImage) # content text if content_txt != ‘‘: msg_content_txt = MIMEText(content_txt,_subtype=‘plain‘,_charset=‘gb2312‘) msg.attach(msg_content_txt) # content html if content_html != ‘‘ and len(picname) == 0: msg_content_html = MIMEText(content_html,_subtype=‘html‘,_charset=‘gb2312‘) msg.attach(msg_content_html) smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.starttls() smtp.login(username, password) smtp.sendmail(msg[‘From‘], msg[‘To‘], msg.as_string()) smtp.quit() def col_name(): all = ‘,‘ for i in uid_name: all = all + get_id(i) + ‘,‘ return all def get_id(idNum): try: return uid[idNum].split(u‘\x00‘)[0].encode(‘gbk‘) except: return str(idNum) zk = win32com.client.Dispatch(‘zkemkeeper.ZKEM.1‘) if not zk.Connect_Net(‘192.168.1.2‘, 4370): print "Connect Error" sys.exit(1) zk.SetDeviceTime(1) #使用PC时间同步到考勤机 if time.localtime()[2] != 1: zk.Disconnect() sys.exit(1) zk.ReadAllUserID(1) uid = {} while 1: exists, idNum, username, other, privilege, enable = zk.GetAllUserInfo(1) if not exists: break else: if enable: uid[idNum] = username checkin = {} last_month = time.localtime()[1]-1 or 12 if last_month == 12: cur_year = time.localtime()[0]-1 else: cur_year = time.localtime()[0] if zk.ReadGeneralLogData(1): #read All checkin data while 1: exists, machNum, idNum, emachNum, verifyMode, outMode, year, month, day, hour, minute = zk.GetGeneralLogData(1) #2 if not exists: break if cur_year == year and last_month == month: if day not in checkin: checkin[day] = {} if idNum in checkin[day]: checkin[day][idNum].append(hour * 60 + minute) else: checkin[day][idNum] = [hour * 60 + minute] zk.Disconnect() csv_name = r‘D:\CheckIn\%s-%s.csv‘ % (cur_year, last_month) uid_name = sorted(uid.keys()) report = col_name() + ‘\n‘ for dayNum in range(1,35): if dayNum not in checkin: break report = report + ‘%s-%s-%s,‘ % (cur_year, last_month, dayNum) for col in uid_name: if col not in checkin[dayNum]: report = report + ‘,‘ continue if len(checkin[dayNum][col]) < 2: report = report + ‘0,‘ else: dayTime = max(checkin[dayNum][col]) - min(checkin[dayNum][col]) if dayTime%60 >= 45: report = report + str(dayTime//60 + 1) + ‘,‘ elif 15 < dayTime%60 < 45: report = report + str(dayTime//60 + 0.5) + ‘,‘ else: report = report + str(dayTime//60) + ‘,‘ report = report + ‘\n‘ write_file(csv_name, report) send_mail(filename=[csv_name], content_txt=‘Check_In %s-%s‘ % (cur_year, last_month))
标签:python 中控考勤机
原文地址:http://abian.blog.51cto.com/751059/1627674