需求:利用PC端微信实现自动向文件传输助手,好友等发送信息

库说明

psutil: 获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息,用于获取进程ID pywinauto:自动化Microsoft Windows GUI的python模块,用于检索微信窗口的控件位置(https://www.kancloud.cn/gnefnuy/pywinauto_doc) pyautogui:PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作用于控制键盘热键 pyinstaller -F xxx.py 最后用打包exe

知识点1:如何获取进程的ID?

可使用psutil 库来查看所有进程的名字,匹配进程名字返回进程ID,,进程名字ID查看工具:inspect.exe:

「inspect.exe」https://www.aliyundrive.com/s/JDSBosz7CfR 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

import psutil

# 获取进程id
def get_pid(processName):
    for proc in psutil.process_iter():
        try:
            if (proc.name() == processName):
                print(proc.name())
                print(str(proc.pid))
                return proc.pid
        except psutil.NoSuchProcess:
            pass
    return -1

知识点2:打开微信可以使用快捷键:ctrl+alt+w

# 打开微信的快捷键
pyautogui.hotkey('ctrl', 'alt', 'w')

 知识点3:利用进程ID实例化

# 利用进程ID初始化一下实例
app = Application(backend='uia').connect(process=procId)
# print(app.window())
# 检索微信窗口
main_Win = app.window(class_name='WeChatMainWndForPC')
# 打印所有的窗口控件信息
# main_Win.print_control_identifiers()
# 最大化
main_Win.maximize()

知识点4:微信中“通信录”,“搜索”等功能实现

微信python自动推送模板 python 微信自动发送_微信python自动推送模板

首先定位“通讯录”元素:main_Win.chind_window()   需要二个参数:title和control_type 

点击该元素click_input()

# 切换到通讯录
btn1 = main_Win.child_window(title="通讯录", control_type="Button").wrapper_object()
# main_Win.Properties.Button3.click()
btn1.draw_outline(colour='red')
btn1.click_input()

 知识点5: 点击微信的搜索功能

# 搜索联系人--文件传输助手
searchKey = ‘张三’
searchEdit = main_Win.child_window(title="搜索", control_type="Edit").wrapper_object()
searchEdit.draw_outline(colour='red')
searchEdit.click_input()
searchEdit.type_keys(searchKey)
sleep(2)

知识点6:搜索后处理

# 选择搜索出的第一个
try:
    selectItem = main_Win.child_window(title=searchKey, control_type="ListItem").wrapper_object()
    selectItem.click_input()
except:
    print("对话框已打开")
# 清空对话框
pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('backspace')

 知识点7:发送消息

# 发送消息
sendMsg = ‘How are you?’
inputMsg = main_Win.child_window(title="输入", control_type="Edit").wrapper_object()
inputMsg.click_input()
inputMsg.type_keys(sendMsg, with_spaces=True)

# 发送
# sendbtn = main_Win["sendBtn"]
# sendbtn.draw_outline(colour='red')
# sendbtn.click_input()
# 回车发送
pyautogui.hotkey('enter')
print(xlsbody[0][i] + ":发送完成")

知识点8:置顶与取消置顶

# 置顶
top = main_Win.child_window(title="置顶", control_type="Button").wrapper_object()
# 定位到置顶元素,画个红色的方框醒目
top.draw_outline(colour='red')
top.click_input()

# 取消置顶
notop = main_Win.child_window(title="取消置顶", control_type="Button").wrapper_object()
notop.draw_outline(colour='red')
notop.click_input()

 参考代码如下【学习使用】

# 改变标准输出的默认编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')


# 获取进程id
def get_pid(processName):
    for proc in psutil.process_iter():
        try:
            if (proc.name() == processName):
                print(proc.name())
                print(str(proc.pid))
                return proc.pid
        except psutil.NoSuchProcess:
            pass
    return -1


# 获取参数
def get_xls():
    # 打开参数表
    print("获取参数中")
    wb = openpyxl.load_workbook("参数.xlsx")
    wscs = wb["参数"]
    groupname = wscs['B1'].value.split(',')  # 转换为列表组名
    groupcoount = len(groupname)
    message = wscs['B2'].value
    atall = wscs['B3'].value
    print(groupname)
    print("共计:" + str(groupcoount) + "组")
    print("发送内容:" + message)
    print("是否@所有人:" + atall)
    return groupname, message, atall, groupcoount


# 获取主窗口
def get_body(procId, xlsbody):
    # 打开微信的快捷键
    pyautogui.hotkey('ctrl', 'alt', 'w')
    # 利用进程ID初始化一下实例
    app = Application(backend='uia').connect(process=procId)
    # print(app.window())
    # 检索微信窗口
    main_Win = app.window(class_name='WeChatMainWndForPC')
    # 打印所有的窗口控件信息
    # main_Win.print_control_identifiers()
    # 最大化
    main_Win.maximize()
    # 置顶
    top = main_Win.child_window(title="置顶", control_type="Button").wrapper_object()
    top.draw_outline(colour='red')
    top.click_input()

    print(main_Win.is_dialog)
    for i in range(xlsbody[3]):
        # 切换到通讯录
        btn1 = main_Win.child_window(title="通讯录", control_type="Button").wrapper_object()
        # main_Win.Properties.Button3.click()
        btn1.draw_outline(colour='red')
        btn1.click_input()
        # 搜索联系人--文件传输助手
        # print(i)
        searchKey = xlsbody[0][i]
        searchEdit = main_Win.child_window(title="搜索", control_type="Edit").wrapper_object()
        searchEdit.draw_outline(colour='red')
        searchEdit.click_input()
        searchEdit.type_keys(searchKey)
        sleep(2)
        # 选择搜索出的第一个
        try:
            selectItem = main_Win.child_window(title=searchKey, control_type="ListItem").wrapper_object()
            selectItem.click_input()
        except:
            print("对话框已打开")
        # 清空对话框
        pyautogui.hotkey('ctrl', 'a')
        pyautogui.hotkey('backspace')
        # if (xlsbody[2] == "是"):
        #     # @所有人
        #     sendMsg = "@"
        #     inputMsg = main_Win.child_window(title="输入", control_type="Edit").wrapper_object()
        #     inputMsg.click_input()
        #     inputMsg.type_keys(sendMsg, with_spaces=True)
        #     allpeople = main_Win.child_window(title="所有人", control_type="Text").wrapper_object()
        #     allpeople.draw_outline(colour='red')
        #     allpeople.click_input()

        # 发送消息
        sendMsg = xlsbody[1]
        inputMsg = main_Win.child_window(title="输入", control_type="Edit").wrapper_object()
        inputMsg.click_input()
        inputMsg.type_keys(sendMsg, with_spaces=True)

        # 发送
        # sendbtn = main_Win["sendBtn"]
        # sendbtn.draw_outline(colour='red')
        # sendbtn.click_input()
        # 回车发送
        pyautogui.hotkey('enter')
        print(xlsbody[0][i] + ":发送完成")
    # 取消置顶
    notop = main_Win.child_window(title="取消置顶", control_type="Button").wrapper_object()
    notop.draw_outline(colour='red')
    notop.click_input()


if __name__ == '__main__':
    procId = get_pid("WeChat.exe")
    if (procId == -1):
        print("微信未运行")
    else:
        get_body(procId, get_xls())