wechat_test.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import win32clipboard as w
  2. import win32con
  3. import win32api
  4. import win32gui
  5. import time
  6. # 把文字放入剪贴板
  7. def setText(aString):
  8. w.OpenClipboard()
  9. w.EmptyClipboard()
  10. w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
  11. w.CloseClipboard()
  12. # 模拟ctrl+V
  13. def ctrlV():
  14. win32api.keybd_event(17, 0, 0, 0) # 按下ctrl
  15. win32api.keybd_event(86, 0, 0, 0) # 按下V
  16. win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放V
  17. win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放ctrl
  18. # 模拟alt+s
  19. def altS():
  20. win32api.keybd_event(18, 0, 0, 0)
  21. win32api.keybd_event(83, 0, 0, 0)
  22. win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
  23. win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)
  24. # 模拟enter
  25. def enter():
  26. win32api.keybd_event(13, 0, 0, 0)
  27. win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
  28. # 模拟鼠标单击
  29. def click():
  30. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  31. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
  32. # 移动鼠标的位置
  33. def movePos(x, y):
  34. win32api.SetCursorPos((x, y))
  35. if __name__ == "__main__":
  36. target_time = ['07:00', '12:00', '22:35'] # 这里是发送时间
  37. name_list = ['覃愉斯'] # 这里是要发送信息的联系人
  38. send_content = "这里是需要发送的信息内容" # 这里是需要发送的信息内容
  39. while True:
  40. now = time.strftime("%m月%d日%H:%M", time.localtime()) # 返回格式化时间
  41. print(now)
  42. if now[-5:] in target_time: # 判断时间是否为设定时间
  43. hwnd = win32gui.FindWindow("WeChatMainWndForPC", '微信') # 返回微信窗口的句柄信息
  44. win32gui.ShowWindow(hwnd, win32con.SW_SHOW) # 激活并显示微信窗口
  45. win32gui.MoveWindow(hwnd, 0, 0, 1000, 700, True) # 将微信窗口移动到指定位置和大小
  46. time.sleep(1)
  47. for name in name_list:
  48. movePos(28, 147)
  49. click()
  50. movePos(148, 35)
  51. click()
  52. time.sleep(1)
  53. setText(name)
  54. ctrlV()
  55. time.sleep(1) # 等待联系人搜索成功
  56. enter()
  57. time.sleep(1)
  58. setText(send_content)
  59. ctrlV()
  60. time.sleep(1)
  61. altS()
  62. time.sleep(1)
  63. win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
  64. time.sleep(60)