微信自動發文字、圖片、文件


一、說明

1.1 微信發文字、圖片、文件一共有五步:

1.鎖定對象;

2.將對象復制剪切板;

3.找到微信句柄;

4.復制到微信;

5.點擊發送。

1.2 微信句柄說明

需要把指定的人(群)拉出來,形成一個單獨的窗口。

 

 

 

 

 

 

1.3 通用代碼

import win32gui, win32api, win32con


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)                # 獲取窗口句柄
    win32gui.GetClassName(hw)                                # 獲取窗口classname
    win32gui.GetWindowText(hw)                               # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")

 

 

二、微信發送文字

2.1 代碼展示

import win32gui, win32api, win32con
import pyperclip


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)                # 獲取窗口句柄
    win32gui.GetClassName(hw)                                # 獲取窗口classname
    win32gui.GetWindowText(hw)                               # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")


sent_text = '這是發送的文字; \n this is character'
pyperclip.copy(sent_text)

if __name__ == '__main__':
    get_handle(to_weixin= '測試人')
    ctrlV()
    altS()

2.2 效果展示

 

 

 

 

 

 

 

 

 

三、發送圖片

3.1 說明

有兩種情況

  1. 截圖之后直接發送
  2. 讀取圖片然后發送

3.2 截圖之后直接發送

3.2.1 截取excel中的區域,然后發送。

3.2.1.1 excel展示

excel文件路徑:E:\測試\ceshi.xlsx

內容展示:

 

 

 

3.2.1.2 代碼展示

使用xlwings來進行截圖操作

import win32gui, win32api, win32con
from time import sleep
from PIL import ImageGrab
import xlwings
import pyperclip


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)                # 獲取窗口句柄
    win32gui.GetClassName(hw)                                # 獲取窗口classname
    win32gui.GetWindowText(hw)                               # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")


def excel_pictures(to_weixin, path_file, pictures_range_list, sheet_list):
    app = xlwings.App(visible=True, add_book=False)
    wb = app.books.open(path_file)
    sleep(1)
    try:
            for i in range(0, len(pictures_range_list)):
                    sheet_name = sheet_list[i]
                    sheet = wb.sheets[sheet_name]
                    range_ = sheet[pictures_range_list[i]]
                    range_.api.CopyPicture()
                    wb.sheets.add('picture')
                    sheet_picture = wb.sheets['picture']
                    sheet_picture.api.Paste()
                    sleep(1)
                    pic = sheet_picture.pictures[0]
                    pic.api.Copy()
                    sleep(1)
                    img = ImageGrab.grabclipboard()
                    sleep(1)
                    get_handle(to_weixin)
                    ctrlV()
                    altS()
                    pic.delete()
                    wb.sheets['picture'].delete()
                    print('#粘貼 成功:%s' % sheet_name)
    except:
        pass
    wb.close()
    app.quit()


def set_text(text):
    pyperclip.copy(text)
    ctrlV()
    altS()


if __name__ == '__main__':
    get_handle(to_weixin='測試人')
    text = '下面是發送從excel中截取圖片的測試'
    set_text(text)
    path_file = r'E:\測試\ceshi.xlsx'
    sheet_list = ['ceshi1', 'ceshi1', 'ceshi2']         # 圖片所在的sheet名稱。
    pictures_range_list = ['A1:C4', 'G10:I13', 'A1:C4'] # 圖片所在的s區域范圍。sheet_list必須與pictures_range_list一一對應
    excel_pictures(to_weixin='測試人', path_file=path_file, sheet_list=sheet_list, pictures_range_list=pictures_range_list)

 

使用win32com來進行截圖操作

import win32gui, win32api, win32con
from time import sleep
from win32com.client import Dispatch
import pyperclip
import os


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)                # 獲取窗口句柄
    win32gui.GetClassName(hw)                                # 獲取窗口classname
    win32gui.GetWindowText(hw)                               # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")


def excel_pictures(to_weixin, path_file, pictures_range_list, sheet_list):
    os.system('taskkill /IM EXCEL.exe /F')    # 殺死正在執行的excel程序,慎用,可不用
    xlapp = Dispatch('Excel.Application')
    wb = xlapp.Workbooks.Open(path_file)
    xlapp.Visible = 1
    xlapp.DisplayAlerts = False  # 必須寫
    sleep(1)
    try:
            for i in range(0, len(pictures_range_list)):
                    sheet_name = sheet_list[i]
                    sheet = wb.Worksheets(sheet_name)
                    range_pic = pictures_range_list[i]
                    sheet.Range(range_pic).CopyPicture()
                    wb.Worksheets.Add().Name = 'picture'
                    sheet_picture = wb.Worksheets('picture')
                    sheet_picture.Range('A1').Select()
                    sheet_picture.Paste()
                    sleep(1)
                    xlapp.Selection.ShapeRange.Name = 'pic_name'
                    sheet_picture.Shapes('pic_name').Copy()
                    sleep(1)
                    # img = ImageGrab.grabclipboard()
                    sleep(1)
                    get_handle(to_weixin)
                    ctrlV()
                    sleep(2)
                    altS()
                    wb.Worksheets('picture').Delete()
                    print('#粘貼 成功:%s', sheet_name)
    except:
        pass
    wb.Close(1)


def set_text(text):
    pyperclip.copy(text)
    ctrlV()
    altS()


if __name__ == '__main__':
    get_handle(to_weixin='測試人')
    text = '下面是發送從excel中截取圖片的測試'
    set_text(text)
    path_file = r'E:\測試\ceshi.xlsx'
    sheet_list = ['ceshi1', 'ceshi1', 'ceshi2']         # 圖片所在的sheet名稱。
    pictures_range_list = ['A1:C4', 'G10:I13', 'A1:C4'] # 圖片所在的s區域范圍。sheet_list必須與pictures_range_list一一對應
    excel_pictures(to_weixin='測試人', path_file=path_file, sheet_list=sheet_list, pictures_range_list=pictures_range_list)

 

 

 

 

3.2.1.3 效果展示

 

 

 

 

 

 

3.2.1.3 補充說明:

excel如果連接了數據庫,是可以自動刷新的。

區域也是可以變化的。

要發送的文字是可以在excel中寫好后發送的。

 

3.3 讀取圖片並發送(注意不是文件)

3.3.1 圖片展示

 

 

 

 

 

3.3.2代碼展示

from PIL import Image
from io import BytesIO
from time import sleep
import win32clipboard as clip
import win32gui, win32api, win32con
import os
import pyperclip


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)  # 獲取窗口句柄
    win32gui.GetClassName(hw)  # 獲取窗口classname
    win32gui.GetWindowText(hw)  # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")


def read_picture(path):
    picture_list = os.listdir(path)
    for picture in picture_list:
        path_file = path + "\\" + picture
        img = Image.open(path_file)
        output = BytesIO()  # BytesIO實現了在內存中讀寫bytes
        img.convert("RGB").save(output, "BMP")  # 以RGB模式保存圖像
        data = output.getvalue()[14:]
        output.close()
        clip.OpenClipboard()  # 打開剪貼板
        clip.EmptyClipboard()  # 先清空剪貼板
        clip.SetClipboardData(win32con.CF_DIB, data)  # 將圖片放入剪貼板
        clip.CloseClipboard()
        ctrlV()
        altS()
        sleep(1)


def set_text(text):
    pyperclip.copy(text)
    ctrlV()
    altS()


if __name__ == '__main__':
    get_handle(to_weixin='測試人')
    text = '下面是發送從文件中讀取圖片的測試'
    set_text(text)
    path = r'E:\測試\ceshi'
    read_picture(path=path)

 

 

 

3.3.3 效果展示

 

 

 

 

 

 

四、 發送文件

4.1 說明

4.1.1 發送文件用的方法和前面有點不一樣,前面都是指定內容,區域,名稱來做的。必要容易控制。

而發送文件是通過指定位置來操作的。

4.1.2 利用PyQt5來給文件建立URL,來操作。參考https://www.cnblogs.com/gujianjian/p/12697716.html

 

4.2 文件展示

4.2.1 說明

排列順序按照名稱來排列,這樣順序不容易改變。不要輕易往里面放文件

把“ceshi2”以文件的形式發送給指定的人(群)

 

 

 

 

 

 

4.3 代碼展示

import  os
from time import sleep
import win32gui, win32api, win32con,win32ui
import pyperclip


def get_handle(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)                # 獲取窗口句柄
    win32gui.GetClassName(hw)                                # 獲取窗口classname
    win32gui.GetWindowText(hw)                               # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)
    print('獲取句柄')


def ctrlV():
    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)
    print("執行粘貼")


def altS():
    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)
    print("執行發送")


def file_copy(path):
    os.system("explorer.exe /n, {}".format(path))
    sleep(10)                                                  # 必須沉睡一段時間,把窗口打開
    win32api.keybd_event(40, 0, 0, 0)                          # 按下向下的按鍵
    win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0)   # 松開向下的按鍵
    win32api.keybd_event(40, 0, 0, 0)
    win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(17, 0, 0, 0)  # ctrl按下
    win32api.keybd_event(67, 0, 0, 0)  # c按下
    win32api.keybd_event(67, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)


def set_text(text):
    get_handle(to_weixin='測試人')
    pyperclip.copy(text)
    ctrlV()
    altS()


if __name__ == '__main__':
    text = '下面是發送文件的測試'
    set_text(text)
    path = r'E:\測試'
    file_copy(path)
    sleep(5)                             #必須沉睡一會
    get_handle(to_weixin = '測試人')
    ctrlV()
    altS()

 

4.4 效果展示

 

4.5 利用PyQt5,建議大家使用這種方法,參考:https://www.cnblogs.com/gujianjian/p/12697716.html

from PyQt5 import QtCore, QtWidgets
import win32gui, win32api, win32con
import time


# 調用win32api的模擬點擊功能實現ctrl+v粘貼快捷鍵
def ctrlV():
    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)


# 調用win32api的模擬點擊功能實現alt+s微信發送快捷鍵 (可以根據自己微信發送快捷鍵是什么來進行調整)
def altS():
    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)


# 調用win32gui調用桌面窗口,獲取指定窗口句柄id,激活窗口  ,向函數傳遞窗口名稱to_weixin
def wx_send(to_weixin):
    hw = win32gui.FindWindow(None, to_weixin)  # 獲取窗口句柄
    win32gui.GetClassName(hw)  # 獲取窗口classname
    title = win32gui.GetWindowText(hw)  # 獲取窗口標題
    win32gui.GetDlgCtrlID(hw)
    win32gui.SetForegroundWindow(hw)  # 激活窗口


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    data = QtCore.QMimeData()
    url = QtCore.QUrl.fromLocalFile(r'E:\測試\ceshi2.xlsx')
    data.setUrls([url])
    app.clipboard().setMimeData(data)
    clipboard = QtWidgets.QApplication.clipboard()
    time.sleep(2)
    wx_send('測試人')
    ctrlV()
    altS()

 


免責聲明!

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



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