importwin32guiimportwin32apiimportwin32conimporttimeimportwin32clipboardimportpyperclipimportpyautoguiwin32api.ShellExecute(0, 'open', r'D:\WeChat\WeChat.exe', '', '', 1)
win=win32gui.FindWindow(None, '微信')
title=win32gui.GetWindowText(win)
print(f'找到{title}主窗口句柄:{win}')
defClipboardText(aString):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, aString)
win32clipboard.CloseClipboard()
time.sleep(1)
win32api.keybd_event(win32con.VK_CONTROL,0,0,0) win32api.keybd_event(ord('V'),0,0,0) win32api.keybd_event(win32con.VK_CONTROL,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(ord('V'),0,win32con.KEYEVENTF_KEYUP,0)defsearch(wxname):
ifwin!=0:
win32gui.SetForegroundWindow(win) win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
win32api.keybd_event(ord('F'), 0, 0, 0)
win32api.keybd_event(ord('F'), 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
ClipboardText(wxname)
time.sleep(1)
win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0)
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0)
else:
print(f'请注意:找不到【{wxname}】这个人(或群)!')
exit()
defSendMsg():
win32api.keybd_event(win32con.VK_MENU, 0, 0, 0) win32api.keybd_event(ord('S'),0,0,0) win32api.keybd_event(win32con.VK_MENU,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(ord('S'),0,win32con.KEYEVENTF_KEYUP,0)defsendText(chatrooms,text):
forchatroominchatrooms:
search(chatroom)
ClipboardText(" "+text)
SendMsg()
print(f'微信消息:{text} 已发送至:{chatroom}')
chatrooms= ['大鹏@Java开发搭建交流群(禁广)', '文件传输助手']
text='世间文字八万个,唯有情字最杀人....'sendText(chatrooms,text)