微信自动发文字、图片、文件

一、说明

1.1 微信发文字、图片、文件一共有五步:

1.锁定对象;

2.将对象复制剪切板;

3.找到微信句柄;

4.复制到微信;

5.点击发送。

1.2 微信句柄说明

需要把指定的人(群)拉出来,形成一个单独的窗口。

技术图片

 

 

 

 

 

 

1.3 通用代码

import win32gui, win32api, win32condef 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, win32conimport pyperclipdef 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 characterpyperclip.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 代码展示

import win32gui, win32api, win32confrom time import sleepfrom PIL import ImageGrabimport xlwingsimport pyperclipdef 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 = rE:\测试\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 Imagefrom io import BytesIOfrom time import sleepimport win32clipboard as clipimport win32gui, win32api, win32conimport osimport pyperclipdef 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 = rE:\测试\ceshi read_picture(path=path)

 

 

 

3.3.3 效果展示

技术图片

 

 

 

 

 

 

四、 发送文件

4.1 说明

发送文件用的方法和前面有点不一样,前面都是指定内容,区域,名称来做的。必要容易控制。

而发送文件是通过指定位置来操作的。

 

4.2 文件展示

4.2.1 说明

排列顺序按照名称来排列,这样顺序不容易改变。不要轻易往里面放文件

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

技术图片

 

 

 

 

 

 

4.3 代码展示

import osfrom time import sleepimport win32gui, win32api, win32con,win32uiimport pyperclipdef 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 = rE:\测试 file_copy(path) sleep(5) #必须沉睡一会 get_handle(to_weixin = 测试人) ctrlV() altS()

 

4.4 效果展示

技术图片

 

相关文章