生活中有时我们需要在特定时间点给别人发送消息时,而又有其他的事情需要处理,很容易造成我们遗忘掉这件事,这是我们就需要一个程序来帮助我们来执行这一任务,经过几天在网上的查询
写了一个小程序,希望各位大神指点一下不足。
1.我们先利用pip下载所需要的模块
pip install time
pip install pyperclip
pip install win32api
pip install win32con
pip install os
2.导入模块,创建是否发送消息的判断条件
import time
import pyperclip
import win32api
import win32con
import os
#判断
while True:
time_now = time.strftime("%H:%M", time.localtime())#获取当前时间
if time_now == "07:44":#此处为消息发送的时间
3.利用绝对路径打开微信
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)#电脑反应需要时间,使程序暂停一段时间来等待电脑反应,单位是秒
4.利用快捷键打开搜索框并在搜索框粘贴所要发送的人
win32api.keybd_event(17, 0, 0, 0)
win32api.keybd_event(70, 0, 0, 0)
win32api.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)
win32api.keybd_event(67, 0, 0, 0)
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(67, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
win32api.keybd_event(13, 0, 0, 0)
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
5.将所需要发送的文字粘贴到文本框并进行发送
pyperclip.copy('聊天内容')
spam = pyperclip.paste()
win32api.keybd_event(17, 0, 0, 0)
win32api.keybd_event(86, 0, 0, 0)
win32api.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(61)#确保程序只执行一次,防止重复执行