目标:
①Python定时发送微信消息;
②Python实时发送微信消息。
使用键位编码win32api、win32con
或者send_keys实现发送微信消息。
首先以win32api、win32con为例:
"""
实现定时自动发送消息
"""import time
import pyperclip
import win32api
import win32con
import oswhile True:time_now = time.strftime("%H:%M:%S", time.localtime()) # 获取当前时间sent_time = '16:57:34' # 发送时间if time_now == sent_time: # 当前时间等于发送时间则执行以下程序def open_app(app_dir):os.startfile(app_dir)# 打开微信if __name__ == "__main__":app_dir = r'C:\Program Files (x86)\Tencent\WeChat\WeChat.exe' # 此处为微信的绝对路径open_app(app_dir)time.sleep(1) #进入微信,模拟按键Ctrl+Fwin32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(70, 0, 0, 0) # Fwin32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)# 复制需要查找的人,按回车,进入聊天输入框pyperclip.copy('请输入你需要聊天的联系人') # 联系人昵称spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→V;win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)win32api.keybd_event(13, 0, 0, 0) # 13→Enterwin32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)#聊天输入框复制聊天内容,然后按回车发送消息pyperclip.copy('Hello~') # 聊天的内容spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→Vwin32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(13, 0, 0, 0)win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1) # 确保程序只执行一次,防止重复执行pyperclip.copy('是时候点餐啦!!') # 聊天的内容spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→Vwin32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(13, 0, 0, 0)win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(3)exit() # 退出程序
运行结果:
实时发送消息:
"""
实时自动发送消息
"""import time
import pyperclip
import win32api
import win32con
import osi=0;
while i<1:i = i + 1;time_now = time.strftime("%H:%M", time.localtime()) # 获取当前时间sent_time = time.strftime("%H:%M", time.localtime()) # 发送时间if time_now == sent_time: # 当前时间等于发送时间则执行以下程序def open_app(app_dir):os.startfile(app_dir)if __name__ == "__main__":app_dir = r'C:\Program Files (x86)\Tencent\WeChat\WeChat.exe' # 此处为微信的绝对路径open_app(app_dir)time.sleep(1) # 电脑反应需要时间,使程序暂停一段时间来等待电脑反应,单位是秒win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(70, 0, 0, 0) # Fwin32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)pyperclip.copy('请输入联系人') # 联系人spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→V; 67→Cwin32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)win32api.keybd_event(13, 0, 0, 0) # 13→Enterwin32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1)pyperclip.copy('Hello~') # 聊天的内容spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→Vwin32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(13, 0, 0, 0)win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(1) # 确保程序只执行一次,防止重复执行pyperclip.copy('是时候点餐啦!!') # 聊天的内容spam = pyperclip.paste()win32api.keybd_event(17, 0, 0, 0) # Ctrlwin32api.keybd_event(86, 0, 0, 0) # 86→Vwin32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(13, 0, 0, 0)win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(5) # 确保程序只执行一次,防止重复执行else:print('停止运行!!')exit()
Python中模拟键盘输入键位编号对照表_baidu_17201253的博客-CSDN博客
发现另外一种更简单的方式,引用send_keys:
"""
实现自动发送消息
"""import time
import os
from pywinauto.keyboard import send_keys #键盘while True:time_now = time.strftime("%H:%M:%S", time.localtime()) # 获取当前时间sent_time = time.strftime("%H:%M:%S", time.localtime()) # 发送时间if time_now == sent_time: # 当前时间等于发送时间则执行以下程序def open_app(app_dir):os.startfile(app_dir)# 打开微信if __name__ == "__main__":app_dir = r'C:\Program Files (x86)\Tencent\WeChat\WeChat.exe' # 此处为微信的绝对路径open_app(app_dir)time.sleep(1)#进入微信,模拟按键Ctrl+Fsend_keys('^f')send_keys('请输入联系人')time.sleep(1)send_keys('{ENTER}') # 回车键必须全部大小#需要发送的消息内容message = '啦啦!!,是时候休息啦!Good night!!!'time.sleep(1)# 输入聊天内容send_keys(message)# 回车发送消息send_keys('{ENTER}')time.sleep(3)print('退出~~~')exit() # 退出程序
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import time
import psutil# 输入进程名,获取PID
def get_pid(p_name):pids = psutil.pids()for pid in pids:p = psutil.Process(pid)if p_name in p.name():return pidchat_name = "测试微信群" # 需要发送消息的聊天名称
message = "message" # 需要发送的消息
we_chat_path = r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe" # 微信路径 C:\Program Files (x86)\Tencent\WeChat# 获取微信PID并获取微信窗口
we_chat_id = get_pid("WeChat.exe")
app = Application(backend='uia').connect(process=we_chat_id)
we_chat_main_dialog = app.window(class_name='WeChatMainWndForPC')# 微信挂在后台时,通过再次运行唤醒
if not we_chat_main_dialog.exists():tmp = Application().start(we_chat_path)# 通过先最小化,再恢复使得窗口置顶
we_chat_main_dialog.minimize()
we_chat_main_dialog.restore()# 通过搜索,定位聊天
search_elem = we_chat_main_dialog.child_window(control_type='Edit', title='搜索')
search_elem.click_input()
search_elem.type_keys('^a').type_keys(chat_name)
time.sleep(1)
send_keys('{ENTER}')# 点击要发送消息的聊天
chat_list = we_chat_main_dialog.child_window(control_type='List', title='会话')
for chat_item in chat_list.items():if chat_name in chat_item.element_info.name:chat_item.click_input()time.sleep(1)# 获取聊天记录
message_list = we_chat_main_dialog.child_window(control_type='List', title='消息')
for message_item in message_list.items():print(message_item)# 输入并发送消息# app["Dialog"]["Edit1"].TypeKeys("E:\\1.jpg")
# app["Dialog"]["Edit1"].TypeKeys("C:\\Users\\Administrator\\Desktop\\用印申请.pdf")
edit_elem = we_chat_main_dialog.child_window(control_type='Edit', title='输入')
edit_elem.type_keys('^a').type_keys(message, with_spaces=True)
time.sleep(1)
send_keys('{ENTER}')
仅用于学习,请勿用于刷屏!!!