1. 程式人生 > 實用技巧 >Python 實現給電腦微信客戶端好友和群聊傳送訊息

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

首先建立一個資料夾image 裡面放入上面的4張圖片均儲存為jpg 名字也要和下面對應一樣。

在建立一個wechat.py寫程式碼

首先在pycharm 下載庫

pyautogui   
pyperclip 
以及opencv-python庫 這個庫下載比較慢 耐心點。想快翻牆
在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()#傳送