# coding = utf-8 import win32api import win32gui import win32con import win32clipboard as clipboard import time from PIL import Image from io import BytesIO #python3,新增字節流 import requests from lxml import etree headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' ' AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/79.0.3945.117 Safari/537.36' } ############################### # 微信發送(文本及圖片) ############################### #定義指定圖片文件複製到剪貼板函數########## def pic_ctrl_c(pathfile): img = Image.open(pathfile) output = BytesIO()#如是StringIO分引發TypeError: string argument expected, got 'bytes' img.convert("RGB").save(output, "BMP")# 以BMP格式保存流 data = output.getvalue()[14:]#bmp文件頭14個字節丟棄 output.close() clipboard.OpenClipboard() #打開剪貼板 clipboard.EmptyClipboard() #先清空剪貼板 clipboard.SetClipboardData(win32con.CF_DIB, data) #將圖片放入剪貼板 clipboard.CloseClipboard() return def send_m(): # 如下爲「CTRL+V」組合鍵,回車發送,(方法一) win32api.keybd_event(17, 0, 0, 0) # 有效,按下CTRL time.sleep(1) # 須要延時 win32gui.SendMessage(win, win32con.WM_KEYDOWN, 86, 0) # V win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) # 放開CTRL time.sleep(1) # 緩衝時間 win32gui.SendMessage(win, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) # 回車發送 return def txt_ctrl_v(txt_str): #定義文本信息,將信息緩存入剪貼板 clipboard.OpenClipboard() clipboard.EmptyClipboard() clipboard.SetClipboardData(win32con.CF_UNICODETEXT,txt_str) clipboard.CloseClipboard() return def day_english(): #獲取金山詞霸每日一句 url = 'http://open.iciba.com/dsapi' r = requests.get(url) content = r.json()['content'] note = r.json()['note'] print(content+note) return content+note def dujitang():#毒雞湯 url = 'https://8zt.cc/' res = requests.get(url=url, headers=headers, timeout=10) selector = etree.HTML(res.text) # content = selector.xpath('//section/div/*/text()')[0] content = selector.xpath('//div[@class="main-wrapper"]//span[@id="sentence"]/text()')[0] #text = str(content) return content #######################發送過程================= #查找微信窗口,若是最小化則還原(須要固定位置) title_name="張三"#須要單獨打開張三的對話框,好友名稱 win = win32gui.FindWindow('ChatWnd',title_name) print("找到句柄:%x" % win) if win!=0: left, top, right, bottom = win32gui.GetWindowRect(win) print(left, top, right, bottom)#最小化爲負數 print("nothe") # #最小化時點擊還原,下面爲單個窗口 if top<0: #鼠標點擊,還原窗口 win32api.SetCursorPos([190, 1040]) # 鼠標定位到(190,1040) # 執行左單鍵擊,若須要雙擊則延時幾毫秒再點擊一次便可 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) ######點擊完成一次 time.sleep(0.5) left, top, right, bottom = win32gui.GetWindowRect(win)#取數 # #最小時點擊還原窗口,下面一節爲多個窗口,依次點擊打開。 k=1040#最小化後的縱座標,橫座標約爲190 while top<0 and k>800:#並設定最多6次,防止死循環 time.sleep(1) win32api.SetCursorPos([180, k-40]) # 鼠標定位菜單第一個 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) ######點擊完成一次 time.sleep(1)#等待窗口出現 left, top, right, bottom = win32gui.GetWindowRect(win)#取數 if top>0 :#判斷是否還原 break else: k-=40#菜單上移一格 win32api.SetCursorPos([190, 1040]) # 從新打開菜單 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32gui.SetForegroundWindow(win)#獲取控制 time.sleep(0.5) else: print('請注意:找不到【%s】這我的(或羣),請激活窗口!'%title_name) # ##開始發送圖片 #pic_ctrl_c(r'C:\Users\Pictures\1.png')#第一張圖片 #time.sleep(1) #send_m() #pic_ctrl_c(r'C:\Users\Pictures\2.png')#第二張圖片 #time.sleep(1) #send_m() #pic_ctrl_c(r'C:\Users\Pictures\3.png')#第三張圖片 #time.sleep(1) #send_m() #開始發送文本 # str=day_english() # txt_ctrl_v(str) # send_m() str2=dujitang() txt_ctrl_v(str2) send_m() str2=dujitang() txt_ctrl_v(str2) send_m() #############################微信發送結束================ #############################其它備註==================== # # #模擬鍵盤CTRL+V,方法二 # keyboard.press(Key.ctrl) # keyboard.press('v')#大寫不行列 # keyboard.release('v') # keyboard.release(Key.ctrl) # ##鼠標操做可忽略 # in_xy=[right-50,bottom-50] # win32api.SetCursorPos(in_xy) # mouse.press(Button.left) # mouse.release(Button.left) # #from pynput.mouse import Button, Controller as mController #from pynput.keyboard import Key, Controller as kController #mouse = mController() #keyboard = kController() #print(type(keyboard)) # keyboard.type(str1) # 發送消息,須要pip install pynput # time.sleep(1)#緩衝時間 # # title_name='微信'#這個不分對象,瞎發。 # win = win32gui.FindWindow('WeChatMainWndForPC',title_name)