Python automatically send information and pictures to the designated micro-letter friends

On one of Python automatically send information and pictures to the designated QQ friends

The principle is not the same, to the use of the class are not the same, QQ automatically get the focus, and not micro-letters, it needs the help of the mouse to click on, so there is little requirement is that the position of micro-letter search box needs to be placed in designated areas, here I set the coordinates (100, 40), and when the search to a friend, click the friends, coordinates (100, 120) position

import os
import win32gui #pywin32-221.win-amd64-py3.7.exe
import win32con
from ctypes import *
import win32clipboard as w
import time
from PIL import Image #pip install pillow
#pip install -i https://pypi.douban.com/simple pillow
import win32api

#发送文字
def setText(info):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT, info)
    w.CloseClipboard()

#发送图片
def setImage(imgpath):
    im = Image.open(imgpath)
    im.save('1.bmp')
    aString = windll.user32.LoadImageW(0, r"1.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE)
 
    if aString != 0:  ## 由于图片编码问题  图片载入失败的话  aString 就等于0
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_BITMAP, aString)
        w.CloseClipboard()  

#微信搜索框不会自动获取焦点,故需要模拟鼠标点击到搜索框的位置
def m_click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def pasteInfo():
    win32api.keybd_event(17,0,0,0)  #ctrl键位码是17
    win32api.keybd_event(86,0,0,0)  #v键位码是86
    win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

#定位微信窗口,进行昵称备注的搜索(需点击两下才能获取到焦点)
def searchByUser(uname):
    hwnd = win32gui.FindWindow('WeChatMainWndForPC', '微信')
    setText(uname)
    m_click(100,40)
    time.sleep(0.5)
    m_click(100,40)
    pasteInfo()
    time.sleep(1)
    m_click(100,120)#搜索到之后点击
    #win32api.keybd_event(13,0,0,0)#回车
    #win32api.keybd_event(13,0,KEYEVENTF_KEYUP,0)
    #win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    #win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)

def sendInfo():
    time.sleep(1)
    pasteInfo()
    time.sleep(1)
    win32api.keybd_event(18, 0, 0, 0) #Alt  
    win32api.keybd_event(83,0,0,0) #s
    win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
    win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)


#发送完信息之后关闭窗口(跟QQ不一样,可以不关闭),接着搜索发送
def closeByUser(uname):
    hwnd = win32gui.FindWindow('WeChatMainWndForPC', '微信')
    win32api.keybd_event(18,0,0,0)  #Alt
    win32api.keybd_event(115,0,0,0) #F4
    win32api.keybd_event(115,0,KEYEVENTF_KEYUP,0)
    win32api.keybd_event(18,0,KEYEVENTF_KEYUP,0)

'''
searchByUser('Tony老师')
setText('Tony老师理发师')
sendInfo()

time.sleep(1)
searchByUser('文件传输助手')
setText('地表最强CPU')
sendInfo()
'''

#获取无后缀的图片名称
def getNosuffixImgName(imgname):
    return os.path.splitext(imgname)[0]

imgdir='imgs/'
imgs=os.listdir(imgdir)
for img in imgs:
    searchByUser(getNosuffixImgName(img))
    setImage(imgdir+img)
    sendInfo()
    time.sleep(1)

-i https://pypi.douban.com/simple pyinstaller the install PIP
pyinstaller -F -w -i xxx.ico yy.py
generated exe file below the directory dist

the keybd_event () Function Prototype 

   VOID keybd_event(

  BYTE bVk, // virtual-key code

  BYTE bScan, // hardware scan code

  DWORD dwFlags, // flags specifying various function options

  DWORD dwExtraInfo // additional data associated with keystroke

  );

As can be seen from the prototype, keybd_event () There are four parameters:

      A first virtual key buttons, such as the Enter key vk_return, tab key is VK_TAB (other specific Appendix: common key table key analog);

      The second parameter is the scan code is generally not provided, instead of the line with 0;

      The third parameter is the option flag, if set to 0 it can keydown, if it is set to keyup "KEYEVENTF_KEYUP";

      The fourth parameter is generally set to 0.

   Alphanumeric key numeric keypad keys function keys other keys 
      bonds codes bonds codes bonds code bond code 
      A 65 0 96 Fl 112 the Backspace. 8 
      B 66. 1 97 F2 of 113 the Tab. 9 
      C 67 2 98 F3 114 the Clear 12 is 
      D the Enter the F4 115. 3 99 68 13 is 
      E 69. 4 the Shift 1 16 16 100 the F5 
      F. 70. 17. 5 101 F6 117 Control 
      G 71 is the Alt. 6 102 1 18 F7 18 is 
      H the F8 72 103 119. 7 Caps Lock 20 is 
      the I 73 is the Esc. 8 120 27 104 the F9 
      J   74        9   105       F10  121      Spacebar      32 
      K   75        *   106       F11  122       Page Up       33 
      L   76        +   107       F12    123       Page Down     34 
      M     77       Enter  108         --   --      End         35 
      N   78       -    109         --   --         Home        36 
      O   79       .    110          --   --         Left Arrow   37 
      P   80       /    111          --   --         Up Arrow     38 
      Q   81       --   --            --   --        Right Arrow     39 
      R   82       --   --            --   --        Down Arrow    40 
      S   83       --   --            --   --         Insert        45 
      T   84       --   --            --   --         Delete       46 
      U   85       --   --           --   --          Help       47 
      V   86       --   --           --   --          Num Lock      144 
      W     87          
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57  

Published 46 original articles · won praise 9 · views 3647

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/102788806