标签:width 句柄 key exp keyevent etc pen win32 变化
1.锁定对象;
2.将对象复制剪切板;
3.找到微信句柄;
4.复制到微信;
5.点击发送。
需要把指定的人(群)拉出来,形成一个单独的窗口。
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("执行发送")
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()
有两种情况
excel文件路径:E:\测试\ceshi.xlsx
内容展示:
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)
excel如果连接了数据库,是可以自动刷新的。
区域也是可以变化的。
要发送的文字是可以在excel中写好后发送的。
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)
发送文件用的方法和前面有点不一样,前面都是指定内容,区域,名称来做的。必要容易控制。
而发送文件是通过指定位置来操作的。
排列顺序按照名称来排列,这样顺序不容易改变。不要轻易往里面放文件
把“ceshi2”以文件的形式发送给指定的人(群)
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()
标签:width 句柄 key exp keyevent etc pen win32 变化
原文地址:https://www.cnblogs.com/qianslup/p/12410042.html