import uiautomation as auto
import numpy as np
import pandas as pd
import re
if __name__=="__main__":
wechatWindow = auto.WindowControl(searchDepth=1, Name="微信", ClassName='WeChatMainWndForPC')
if wechatWindow.SetFocus(): # 正确返回self.Element.SetFocus() 错误返回False
session = wechatWindow.ListControl(Name="会话")
we = session.TextControl(searchDepth=4)#深度为4
while True:
if we.Exists():
wechatWindow.ListControl()#
wechatWindow.SwitchToThisWindow()
df = pd.read_csv('回复数据.csv', encoding='utf-8')# 通过pd读取数据
we.Click(simulateMove=False)# 点击未读消息
last_msg = wechatWindow.ListControl(Name='消息').GetChildren()[-1].Name# 读取最后一条消息
# 判断关键字,#axis按行应用函数 默认0按列
msg = df.apply(lambda x: x['回复内容'] if x['关键词'] in last_msg else None, axis=1)
#如果last_msg有关键词则x=回复内容,没有的则变成
# 数据筛选,移除空数据
msg.dropna(axis=0, how='any', inplace=True)#axis=0时,删除有空值的那一行 how='all'则删除空值的行和列
ar = np.array(msg).tolist()#做成列表
# 能够匹配到数据时
if ar:
# 发送消息 回车键
wechatWindow.SendKeys(ar[0],waitTime=0)
wechatWindow.SendKeys('{Enter}', waitTime=0)
# 没有匹配到数据时
else:
wechatWindow.SendKeys('我不理解你什么意思', waitTime=1)
wechatWindow.SendKeys('{Enter}', waitTime=0)
wechatWindow.ButtonControl(Name="最小化" ).Click()
回复数据.csv里面的内容
序号,关键词,回复内容
1,早上好,早上好,我在吃早餐