python自动化操作模型——excel刷新数据并发送微信
程序员文章站
2022-05-04 11:24:36
1 import os 2 import win32gui, win32api, win32con, win32com 3 from win32com.client import Dispatch 4 from PIL import ImageGrab,Image 5 from time impor ......
1 import os 2 import win32gui, win32api, win32con, win32com 3 from win32com.client import dispatch 4 from pil import imagegrab,image 5 from time import sleep 6 import pyperclip 7 8 # 调用win32api的模拟点击功能实现ctrl+v粘贴快捷键 9 def ctrlv(): 10 win32api.keybd_event(17,0,0,0) #ctrl键位码是17 11 win32api.keybd_event(86,0,0,0) #v键位码是86 12 win32api.keybd_event(86,0,win32con.keyeventf_keyup,0) #释放按键 13 win32api.keybd_event(17,0,win32con.keyeventf_keyup,0) 14 15 # 调用win32api的模拟点击功能实现alt+s微信发送快捷键 (可以根据自己微信发送快捷键是什么来进行调整) 16 def alts(): 17 win32api.keybd_event(18, 0, 0, 0) #alt 18 win32api.keybd_event(83,0,0,0) #s 19 win32api.keybd_event(83,0,win32con.keyeventf_keyup,0) #释放按键 20 win32api.keybd_event(18,0,win32con.keyeventf_keyup,0) 21 22 # 调用win32gui调用桌面窗口,获取指定窗口句柄id,激活窗口 ,向函数传递窗口名称to_weixin 23 def wx_send(to_weixin): 24 for i in range(0,len(to_weixin)): 25 hw = win32gui.findwindow(none, to_weixin[i]) # 获取窗口句柄 26 win32gui.getclassname(hw) # 获取窗口classname 27 title = win32gui.getwindowtext(hw) # 获取窗口标题 28 win32gui.getdlgctrlid(hw) 29 win32gui.setforegroundwindow(hw) # 激活窗口 30 sleep(1) 31 ctrlv() 32 sleep(1) 33 alts() 34 35 # 使win32调用excel,刷新数据,并发送微信,refreshall刷新excel的所有数据来源,我的数据来源是pq搭建的链接数据库的模型,也可以搭载爬虫的数据,这样就可以完成自动刷新数据并发送微信的操作,解放双手更多的时间来学习 36 def wkb_operate(class_picture,wkb_path,sleep_time): 37 os.system('taskkill /im excel.exe /f') 38 xlapp = win32com.client.gencache.ensuredispatch('excel.application') 39 xlapp.visible = 1 40 xlapp.displayalerts = false # 关闭警告 41 wkb = xlapp.workbooks.open(wkb_path) 42 wkb.refreshall() 43 sleep(sleep_time) 44 print('文件【{}】已打开!'.format(wkb_path)) 45 try: 46 for key,vlaue in class_picture.items(): 47 48 to_weixin = class_picture[key]['发送群'] 49 to_sontent = class_picture[key]['发送文本'] 50 sheet_name = class_picture[key]['sheetname'] 51 range_pic = class_picture[key]['图片区域'] 52 53 pyperclip.copy(to_sontent) 54 wx_send(to_weixin) 55 56 sheet_msg = wkb.worksheets(sheet_name) 57 sheet_msg.range(range_pic).copypicture() 58 wkb.worksheets.add().name = 'picture' 59 sheet_picture = wkb.worksheets('picture') 60 sleep(1) 61 sheet_picture.range('a1').select() 62 sheet_picture.paste() 63 sleep(1) 64 xlapp.selection.shaperange.name = 'pic_name' 65 sheet_picture.shapes('pic_name').copy() 66 sleep(1) 67 img = imagegrab.grabclipboard() 68 sleep(1) 69 wx_send(to_weixin) 70 wkb.worksheets('picture').delete() 71 print('#粘贴 成功:%s',sheet_name) 72 except baseexception as e: 73 print(e) 74 pass 75 wkb.save() 76 wkb.close(1) 77 xlapp.quit() 78 print('#更新 成功:%s' % wkb_path) 79 pass 80 # *********************主程序,大致方向是,1、调用wkb_operate刷新函数刷新exce数据,并返回文本数据播报内容, 81 #2、调用wx_send激活微信窗口,3、ctrlv(),alts()模拟粘贴发送功能,4、调用excel_picture函数,截图到剪切板上,并调用wx_send(),ctrlv(),alts()微信发送图片 82 # 微信发送窗口,必须保持一致,按照此名字识别,否则发送不会成功****** 83 84 # 文件路径: 85 path_process = r"c:\users\1\desktop\备份\ytshuaxin\hour\2020年新流水监控表.xlsx" # 文件夹路径 86 87 #*********主程序***************# 88 class_picture1 = {'pic1':{'发送群':['管理团队','数据中心'], 89 'sheetname':'日监控', 90 '图片区域':'a1:al50', 91 '发送文本':'截止到目前的流水和pk情况'}} 92 result_sheet_value = wkb_operate(class_picture1,path_process,8)