Python自动发送微信消息

一、用Python自动发送微信消息

import itchat

# enableCmdQR=True,允许在cmd命令行显示二维码
# hotReload=True,允许短期内可以不需要重复登陆
itchat.auto_login(enableCmdQR=True,hotReload=True)

# to_name = itchat.search_friends(name="微信好友备注名称")
# print(to_name)
# # 发送文本消息
# for i in range(10):
#     itchat.send('测试中。。。',toUserName=to_name[0]['UserName'])
#
# # 发送表情包
# file_img = 'biaoqingbao.jpg'
# itchat.send_image(file_img,toUserName=to_name[0]['UserName'])

# search_chatrooms 获取通讯录中群聊列表 update=True 会获取实时有信息的群
# myroom = itchat.get_chatrooms(update=True)
# print(myroom)

to_room = itchat.search_chatrooms(name='群聊名称')
# print(to_room)
for i in range(10):
    itchat.send('需要发送的文字信息',toUserName=to_room[0]['UserName'])

二、用Python自动接收 并 回复微信消息,同时把接收的文件进行自动保存

from itchat.content import *

# 判断收到的信息类型,如果收到的信息是文本,则执行下面的函数
@itchat.msg_register(TEXT)
def text_reply(msg):
    print(msg.text)
    reply_text = msg.text.replace('吗?','!')
    # print(reply_text)
    return reply_text

# @itchat.msg_register([PICTURE,RECORDING,ATTACHMENT,VIDEO])
# def download_files(msg):
#     print(msg)
#     msg.download(msg.fileName)

itchat.auto_login(enableCmdQR=True,hotReload=True)

# 保证程序一直在运行
itchat.run()

三、在文件被修改后实时发送修改的信息给指定的微信好友

import os
import time
import xlrd
import itchat

itchat.auto_login(enableCmdQR=True,hotReload=True)
change_time_save = time.ctime(os.stat('aaa.xlsx').st_mtime)

while True:
    time.sleep(5)
    change_time = time.ctime(os.stat('aaa.xlsx').st_mtime)
    if change_time_save == change_time:
        pass
    else:
        change_time_save = change_time
        xlsx = xlrd.open_workbook('aaa.xlsx')
        table = xlsx.sheet_by_index(0)
        content = str(table.cell_value(0,0))
        to_name = itchat.search_friends(name='微信好友备注名')
        itchat.send(content,toUserName=to_name[0]['UserName'])
        print("发送成功!时间:" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))

你可能感兴趣的:(Python自动化办公,python,itchat,Python,itchat,微信推送,微信自送发送消息,微信自动回复)