432ewd 发表于 2015-4-3 09:01:14

python连接中控考勤机分析数据

用python连接中控考勤机。 下载并分析数据,把结果邮件给人事。

中控SDK包: x32地址 x64地址
SDK包建议用32位的,在win7 64位系统上用64位开发包不行,用32可以。

python还要pywin32 注意版本,我这用的 32位的python 2.7 然后下的这个pywin32


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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, '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.split(u'\x00').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() != 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 = username

checkin = {}
last_month = time.localtime()-1 or 12
if last_month == 12:
    cur_year = time.localtime()-1
else:
    cur_year = time.localtime()

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 = {}
            if idNum in checkin:
                checkin.append(hour * 60 + minute)
            else:
                checkin =

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:
            report = report + ','
            continue
      if len(checkin) < 2:
            report = report + '0,'
      else:
            dayTime = max(checkin) - min(checkin)
            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=, content_txt='Check_In %s-%s' % (cur_year, last_month))



页: [1]
查看完整版本: python连接中控考勤机分析数据