標籤:python 中控考勤機
用python串連中控考勤機。 下載並分析資料,把結果郵件給人事。
中控SDK包: x32地址 x64地址
SDK包建議用32位的,在win7 64位系統上用64位開發包不行,用32可以。
python還要pywin32 注意版本,我這用的 32位的python 2.7 然後下的這個pywin32
#!/usr/bin/env python#_*_ coding:gbk _*_import win32com.clientimport timeimport sysimport smtplibfrom 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 = ‘[email protected]‘ password = ‘abc123‘ msg = MIMEMultipart() msg[‘Subject‘] = ‘Check_In‘ msg[‘From‘] = "[email protected]" msg[‘To‘] = "[email protected]" # 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] = usernamecheckin = {}last_month = time.localtime()[1]-1 or 12if last_month == 12: cur_year = time.localtime()[0]-1else: 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串連中控考勤機分析資料