转载自己的文章:Python之微信自动化 • 威远博客,威远工作室,Ease
Python的插件(组件)是相当多的,自动化方面也是。
虽然基于客户端微信去实现各种功能有点尴尬的感觉,但总还是比没有强。我总习惯一个问题寻找多种可能的解决方法,哪怕不是最优解。
能做个啥呢?比如把微信转换为语音播放,赞赏码付费信息处理等。
这里主要使用了两个库:pyautogui主要用于鼠标键盘的操作,uiautomation主要用于Win下程序的组件句柄控制。
uiautomation 模块中文说明
以下代码功能实现的是发现新收到的消息,程序部份思路还有问题。不适合复制粘贴。
import pyautogui as pauto
import uiautomation as uauto
import time
pic = ['wxd0.png','wxd1.png', 'wx10.png','wx11.png','wx12.png','wx21.png','wx30.png','wx31.png']
chat_content = []
def wx_mess():
for p in pic:
#print(p)
a = pauto.locateOnScreen(p)
if not a is None:
x, y = pauto.center(a) # 获得文件图片在现在的屏幕上面的中心坐标
pauto.moveTo(x,y,duration=0) # 移动到图像位置
pauto.move(-20,10) # 图标偏移
pauto.click() # 点击
while True:
time.sleep(3)
wx=uauto.WindowControl(Name='微信',searchDepth=1) # 绑定微信的主程序窗口
wx.SetActive() # 激活程序
search = wx.EditControl(Name='搜索')
edit = wx.EditControl(Name='输入')
messages = wx.ListControl(Name='消息')
sendButton = wx.ButtonControl(Name='sendBtn')
chat = wx.ListControl(Name='会话')
# 是否有新消息
new_content = messages.GetChildren()[-1].Name
has = True
for c in chat_content:
# 已经包含了此句,不是新消息
if c==new_content:
has = False
continue
# 是新消息,添加到列表中
if has:
chat_content.append(new_content)
print("新消息:", new_content)
wx_mess()
time.sleep(10)