自动验证学生信息,回复相应的成绩
数据存储在xls中,使用xlrd,和xlwt读写
import itchat
import os
import PIL.Image as Image
from os import listdir
import math
from itchat.content import *
import pickle
import xlrd, xlwt
print('请扫码登录...')
# 登录,会弹出二维码
itchat.auto_login(hotReload=True)
# 验证学号和身份证
def check(num=0, id=0):
data = xlrd.open_workbook('info.xlsx')
print(data)
table = data.sheet_by_index(0) # 通过索引顺序获取
table = data.sheet_by_name(u'Sheet1') # 通过名称获取
table = data.sheets()[0] # 通过索引顺序获取
# 获取行数和列数
nrows = table.nrows
ncols = table.ncols
print(nrows, ncols)
# 循环行列表数据,寻找符合条件的数据
for i in range(nrows):
tnum = int(table.row_values(i)[2])
tid = table.row_values(i)[3].strip().lower()
if int(num) == tnum and id == tid:
print(tnum, tid)
return True
return False
# 根据学号获取成绩
def getScore(num):
data = xlrd.open_workbook('score.xlsx')
print(data)
table = data.sheet_by_index(0) # 通过索引顺序获取
table = data.sheet_by_name(u'Sheet1') # 通过名称获取
table = data.sheets()[0] # 通过索引顺序获取
# 获取行数和列数
nrows = table.nrows
ncols = table.ncols
print(nrows, ncols)
# 循环行列表数据
for i in range(nrows):
tnum = int(table.row_values(i)[0])
if int(num) == tnum:
# 成绩为是整数,且放置在第3列
s = int(table.row_values(i)[3])
print(tnum, s)
return s
# 根据学号获取姓名
def getName(num):
data = xlrd.open_workbook('score.xlsx')
print(data)
table = data.sheet_by_index(0) # 通过索引顺序获取
table = data.sheet_by_name(u'Sheet1') # 通过名称获取
table = data.sheets()[0] # 通过索引顺序获取
# 获取行数和列数
nrows = table.nrows
ncols = table.ncols
print(nrows, ncols)
# 循环行列表数据
for i in range(nrows):
tnum = int(table.row_values(i)[0])
if int(num) == tnum:
s = table.row_values(i)[1]
print(tnum, s)
return s
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
username = msg["FromUserName"]
# 捕获所有异常,比如数据不符合格式,或者表情包之类的
try:
num = int(msg['Text'].split()[0])
id = msg['Text'].split()[1].strip().lower()
if (check(num, id)):
str = '姓名:%s\n成绩:%d' % (getName(num), getScore(num))
itchat.send(str, toUserName=username)
else:
itchat.send('请输入正确信息...', toUserName=username)
except Exception:
itchat.send('不要乱输无关信息...', toUserName=username)
itchat.run()
来源:oschina
链接:https://my.oschina.net/u/2856757/blog/1560323