Python 實現給電腦微信客戶端好友和群聊發送消息


首先創建一個文件夾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()#發送

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM