Python3 itchat实现微信定时发送群消息的实例代码

yipeiwu_com6年前Python基础

一、简介

1,使用微信,定时往指定的微信群里发送指定信息。

2,需要发送的内容使用excel进行维护,指定要发送的微信群名、时间、内容。

二、py库

1,itchat:这个是主要的工具,用于连接微信个人账号接口。以下是一些相关的知识点网站。

2,xlrd:这个是用来读Excel文件的工具。

3,apscheduler:这个是用来定时调度时间的工具。

三、实例代码

# coding=utf-8
from datetime import datetime
import itchat
import xlrd
from apscheduler.schedulers.background import BlockingScheduler
import os
def SentChatRoomsMsg(name, context):
  itchat.get_chatrooms(update=True)
  iRoom = itchat.search_chatrooms(name)
  for room in iRoom:
    if room['NickName'] == name:
      userName = room['UserName']
      break
  itchat.send_msg(context, userName)
  print("发送时间:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n"
    "发送到:" + name + "\n"
    "发送内容:" + context + "\n")
  print("*********************************************************************************")
  scheduler.print_jobs()
def loginCallback():
  print("***登录成功***")
def exitCallback():
  print("***已退出***")
itchat.auto_login(hotReload=True, enableCmdQR=True, loginCallback=loginCallback, exitCallback=exitCallback)
workbook = xlrd.open_workbook(
  os.path.join(os.path.dirname(os.path.realpath(__file__)), "chatroomsfile\AutoSentChatroom.xlsx"))
# workbook = xlrd.open_workbook("D:\PyCharmCode\AutoLiulishouWechat\chatroomsfile\AutoSentChatroom.xlsx")
sheet = workbook.sheet_by_name('Chatrooms')
iRows = sheet.nrows
scheduler = BlockingScheduler()
index = 1
for i in range(1, iRows):
  textList = sheet.row_values(i)
  name = textList[0]
  context = textList[2]
  float_dateTime = textList[1]
  date_value = xlrd.xldate_as_tuple(float_dateTime, workbook.datemode)
  date_value = datetime(*date_value[:5])
  if datetime.now() > date_value:
    continue
  date_value = date_value.strftime('%Y-%m-%d %H:%M:%S')
  textList[1] = date_value
  scheduler.add_job(SentChatRoomsMsg, 'date', run_date=date_value,
    kwargs={"name": name, "context": context})
  print("任务" + str(index) + ":\n"
    "待发送时间:" + date_value + "\n"
     "待发送到:" + name + "\n"
    "待发送内容:" + context + "\n"
     "******************************************************************************\n")
  index = index + 1
if index == 1:
  print("***没有任务需要执行***")
scheduler.start()

总结

以上所述是小编给大家介绍的Python3 itchat实现微信定时发送群消息的实例代码 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Tensorflow环境搭建的方法步骤

Tensorflow环境搭建的方法步骤

What? 我们需要通过VMware虚拟机平台+Ubuntu虚拟机+安装pip的方式来搭建TensorFlow的环境。 官网参考文档地址:https://www.tensorflow.o...

opencv python 傅里叶变换的使用

opencv python 傅里叶变换的使用

理论 傅立叶变换用于分析各种滤波器的频率特性,对于图像,2D离散傅里叶变换(DFT)用于找到频域.快速傅里叶变换(FFT)的快速算法用于计算DFT. 于一个正弦信号,x(t)=Asin(...

python微信跳一跳系列之棋子定位像素遍历

python微信跳一跳系列之棋子定位像素遍历

前言 在前几篇博客中,分别就棋子的颜色识别、模板匹配等定位方式进行了介绍和实践,这一篇博客就来验证一下github中最热门的跳一跳外挂中采用的像素遍历的方法。 方法说明 像素遍历的实质依...

python操作MongoDB基础知识

首先运行easy_install pymongo命令安装pymongo驱动。然后执行操作:创建连接复制代码 代码如下:In [1]: import pymongoIn [2]: conn...

pytorch程序异常后删除占用的显存操作

1-删除模型变量 del model_define 2-清空CUDA cache torch.cuda.empty_cache() 3-步骤2(异步)需要一定时间,设置时延...