一、簡介
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實現微信定時發送群消息的實例代碼 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

