Python 实现给电脑微信客户端好友和群聊发送消息

时间:2024-03-09 18:01:48

首先创建一个文件夹image 里面放入上面的4张图片均保存为jpg  名字也要和下面对应一样。

 

 在创建一个wechat.py写代码

 

 首先在pycharm 下载库 

pyautogui   
pyperclip 
以及opencv-python库 这个库下载比较慢 耐心点。想快FQ
在wechat写入代码:
import cv2 as cv
import pyautogui
import pyperclip
import os
import time

from PIL import ImageGrab

class wechat():
    def __init__(self,wechat_path):

        self.wechat_path = wechat_path

    path = os.getcwd().replace(\'\\\', \'/\')
    def scanner_img(self,name):
        clip = ImageGrab.grab()
        # clip = ImageGrab.grabclipboard()

        if (clip != None):

            clip.save(self.path+"/image/" + name + ".jpg")
            return self.path+"/image/" + name + ".jpg"
        return "err"

    def find_image(self,obj_path, src_path):
        source = cv.imread(src_path)
        template = cv.imread(obj_path)
        result = cv.matchTemplate(source, template, cv.TM_CCOEFF_NORMED)

        pos_start = cv.minMaxLoc(result)[3]

        x = int(pos_start[0]) + int(template.shape[1] / 2)
        y = int(pos_start[1]) + int(template.shape[0] / 2)
        similarity = cv.minMaxLoc(result)[1]
        print(x,y)
        if similarity < 0.85:
            return (x, y)
        else:
            return (x, y)

    def check_img(self,obj_path, src_path):
        x = -1
        y = -1
        while x == -1:
            x, y = self.find_image(obj_path, src_path)
            print(x,y)
        return (x, y)

    \'\'\'
        打开微信(需要用户自己截取屏幕上的微信图片)
    \'\'\'
    def open_wechat_cv(self):
        pyautogui.hotkey(\'win\', \'d\')
        # res_name = gitBigImg.git_src_img("temp")
        res_name = self.scanner_img("temp")
        src_path = res_name
        #print(src_path)
        obj_path = self.path+\'/image/wechat.jpg\'
        x, y = self.check_img(obj_path, src_path)

        # pyautogui.hotkey(\'win\',\'d\')
        time.sleep(1)
        # pyautogui.moveTo(res[0][0],res[0][1],duration=2)
        pyautogui.doubleClick(x, y)

    def open_wechat(self):
        pyautogui.hotkey(\'win\', \'d\')
        cmd = self.wechat_path+\'\'
        file = os.popen(cmd)
        #print(file)
        file.close()
        time.sleep(2)

    def send_msg_obj(self,name):
        # res_name = gitBigImg.git_src_img("temp2")
        res_name = self.scanner_img("temp2")

        src_path = res_name
        # 寻找搜索图片
        obj_path = self.path+\'/image/lan.jpg\'
        x, y = self.find_image(obj_path, src_path)
        #print(x,y)
        pyautogui.click(x, y)
        time.sleep(1)

        pyperclip.copy(name)

        pyautogui.hotkey(\'ctrl\', \'v\')
        time.sleep(1)
        #print(x,y)
        pyautogui.hotkey(\'enter\')

    \'\'\'
    需要发送的信息文本
    \'\'\'
    def send_msg(self,text):
        pyperclip.copy(text)
        pyautogui.hotkey(\'ctrl\', \'v\')

    \'\'\'
    微信换行
    \'\'\'
    def huang_hang(self):
        pyautogui.hotkey( \'enter\')
    \'\'\'
    信息发送
    \'\'\'
    def send(self):
        pyautogui.hotkey(\'enter\')

if __name__ == \'__main__\':
    #使用class时,需要初始化微信的所在位置
    shuru = int(input(\'请选择发送的对象:1:文件传输助手 2.xxx 3.xxx 4.xxx 5.xxx \n请选择:\'))
    if shuru == 1:
        duixiang = \'文件传输助手\' #测试用
    if shuru == 2:
        duixiang = \'xxx\'#填要发送的人名字 或者群名字
    if shuru == 3:
        duixiang = \'xxx\'
    if shuru == 4:
        duixiang = \'xxx\'
    if shuru == 5:
        duixiang = \'xxx\'
    fileName = \'xxx.txt\'#这里可以读取文件内容发送给别人
    with open(fileName, "r",encoding=\'UTF-8\') as f:  # 设置文件对象
        str = f.read()  # 可以是随便对文件的操作
    fileName1 = \'xxx.txt\'#这里可以读取文件内容发送给别人
    with open(fileName1, "r",encoding=\'UTF-8\') as f:  # 设置文件对象
        str1 = f.read()  # 可以是随便对文件的操作
    wechat = wechat(\'C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe\')
    wechat.open_wechat()
    wechat.send_msg_obj(duixiang)#这里修改可以指定发送给某人
    wechat.send_msg(str)#发送的内容,可以随便自己填,不一定文本的
    wechat.huang_hang()
    wechat.send_msg(str1)#发送的内容,可以随便自己填,不一定文本的
    wechat.send()#发送