最近要写一个python脚本实现读取在线文档上的内容,并每天定时发送其中的内容到企微机器人,实现每天值班人员定时提醒。基本思路是使用BeautifulSoup库将在线文档定时下载到本地后,进行处理;但陷入的问题是无法自动化登录像企微文档、腾讯在线文档,不登录就无法下载!奈何我太菜,tx的验证又太多。。。后来发现可以使用webdrive,但是这种情况下自动登录只能在图形化界面系统中。。。
还是使用本地文件吧,每个月往服务器上传一下就OK了,以后找到好的实现方法补。
主要实现两个功能,一是往其中一个群里发送今天值班人员和明天值班人员并@相关人员,二是往另一个群发送两个值班人并@
既然多个使用就写个class
使用的Excel表格格式如下:
centos7上安装python3
from openpyxl import Workbook,load_workbook
import datetime
import requests
class dutyCall:
def __init__(self,mobile_number,duty_filename,webhook):
self.mobile_number = mobile_number #人名-号码获取表
self.mobile_number_list = [] #需要@的人手机号集合
self.duty_filename = duty_filename #读取的Excel文件位置
self.webhook =webhook #企微机器人地址
def haveOne(self,name): #检测该事项值班是否为空或——
if name == "——" or name == "None":
return 0
else:
return 1
def name2mobileNumber(self,name): #将值班人姓名转化为其手机号 用于@
# mobile_number_list =[]
while name.find(',') > -1:
name = name.replace(',', ',')
while name.find('(') > -1:
name = name.replace('(', '(')
if name.find(',') > -1:
onename = name.split(',')
else:
namestr = name
onename = []
onename.append(str(namestr))
for one in onename:
if one.find('(') > -1:
name_real = one[0:one.find('(')]
else:
name_real = one
# print(name_real)
if self.number2( str(self.mobile_number[name_real])) == 0:
self.mobile_number_list.append(str(self.mobile_number[name_real]))
#return self.mobile_number_list
def number2(self, number): # 判断@号码表是否已有
if self.mobile_number_list == []:
return 0
for n in self.mobile_number_list:
if n == number:
return 1
return 0
def to_robot(self,message): #执行发送信息给机器人
header = "Content-Type: application/json"
data = {
"msgtype": "text",
"text": {
"content": message,
"mentioned_mobile_list": self.mobile_number_list
}
}
print(self.mobile_number_list)
r = requests.post(url=self.webhook, json=data)
print(r.text)
def dothings(self): #进行读取Excel,并调用发送函数 发送
wb = load_workbook(self.duty_filename)
sheet = wb['Sheet1']
rows = sheet.max_row
cols = sheet.max_column
# print(cols)
date = str(datetime.datetime.now())[0:10] # 截取时间 xxxx-xx-xx
# date = "2023-03-09"
print(date)
today = '**今日值班:**'
tomm = '**明日值班:**'
for i in range(1, rows + 1):
if date == str(sheet.cell(i, 1).value)[0:10]: # 匹配当日 行
for x in range(2, cols + 1): # 该日每列遍历
if self.haveOne(str(sheet.cell(i, x).value)): # 检测当前列今日是否有人
today = today + "\n" + str(sheet.cell(2, x).value) + ":" + str(sheet.cell(i, x).value)
self.name2mobileNumber(str(sheet.cell(i, x).value))
for y in range(2, cols + 1):
if self.haveOne(str(sheet.cell(i + 1, y).value)):
tomm = tomm + "\n" + str(sheet.cell(2, y).value) + ":" + str(sheet.cell(i + 1, y).value)
print(today + "\n" + tomm)
text = today + "\n" + tomm
self.to_robot(text)
def dothing_g4(self): #同dothings 但只发送几天且前两列的信息
wb = load_workbook(self.duty_filename)
sheet = wb['Sheet1']
rows = sheet.max_row
cols = sheet.max_column
# print(cols)
date = str(datetime.datetime.now())[0:10] # 截取时间 xxxx-xx-xx
# date = "2023-03-09"
print(date)
today = '**今日值班:**'
for i in range(1, rows + 1):
if date == str(sheet.cell(i, 1).value)[0:10]: # 匹配当日 行
for x in range(2, 4): # 该日每列遍历
if self.haveOne(str(sheet.cell(i, x).value)): # 检测当前列今日是否有人
today = today + "\n" + str(sheet.cell(2, x).value) + ":" + str(sheet.cell(i, x).value)
self.name2mobileNumber(str(sheet.cell(i, x).value))
print(today )
text = today
self.to_robot(text)
#企微机器人webhook
webhookyw = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxx-xxx-xxxx-xxxxxxxxxxx"
# webhookyw="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
#webhookg4="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxxx-4804-xxxx-xxxxxxxxxx"
mobile_number = { #对照表人名-手机号
'aa':'11111111',
'bbb':'22222',
'cc':'3333',
'dd':'44',
'ee':'55',
'ff':'66',
'gg':'77',
'hh':'88'
}
duty_filename = 'C:/Users/Administrator/Desktop/值班.xlsx' #值班execl地址
#duty_filename = '/zihban/值班.xlsx'
dutyCalleryw =dutyCall(mobile_number=mobile_number,duty_filename=duty_filename,webhook=webhookyw)
dutyCalleryw.dothings()
dutyCallerg4 =dutyCall(mobile_number=mobile_number,duty_filename=duty_filename,webhook=webhookg4)
dutyCallerg4.dothing_g4()
01 09 * * * python3 /zihban/fin.py &>> /zihban/zbpy.log
#每天9:01 发送消息