Python自动化操作电脑微信实现发送图片,读取微信聊天记录,自动回复功能

想搞点自动化,想了下市面上PC的自动化用RPA比较多,但RPA限制太多了。
就想着自己用Python写一个能操作微信,可以做到自动回复,自动读取聊天记录,等等的一些操作的自动化代码。
目前我利用这个Python实现的功能有,使用微信PC端,搜索群聊,发送内容,检测内容是否发送成功,以及读取群聊天记录的。
运行效果如下。

使用Python自动化操作电脑微信,实现自动回复,发送图片功能

实现原理

这里主要使用到了uiautomation库,还有UISpy.exe这些工具包括源代码我都会打包给各位的。
具体代码最终我都会放出来给各位的,如果各位对这个代码有疑问,可以咨询我微3957165
自动化需要发送的图片如下。
在这里插入图片描述

自动化操作微信页面代码。

import time
import uiautomation as auto
import pyperclip
import os
import subprocess
#引入打印错误日志信息包
import traceback


def weChat(groupName,index=0):
    if index == None or index == 0:
        index = 0
    # 连接到微信应用的主窗口
    wechat_window = auto.WindowControl(searchDepth=1, Name="微信", ClassName="WeChatMainWndForPC")
    # 检查微信窗口是否存在
    if not wechat_window.Exists(5,1):
        print("微信窗口没有找到")
        return   
    def getS():
        wechat_window.SetActive() # 激活窗口            
        # 寻找同级下面的
        # 定位到 ToolBarControl
        tab_control = wechat_window.ToolBarControl(searchDepth=3)
        print(tab_control)
        # 获取toolbar的父控件
        parent_control = tab_control.GetParentControl()

        # 检查是否找到父级控件
        if not parent_control.Exists():
            print("没有找到父级控件。")
            return
        # 获取父控件的所有子控件
        sibling_controls = parent_control.GetChildren()    
        target_control = sibling_controls[1]    
        children = target_control.GetChildren()    
        first_child_control = children[0]
        
        
        text_control = first_child_control.TextControl(searchDepth=3,ClassName="", AutomationId="", Name="")  # 按条件修改
        
        # 设置焦点
        text_control.SetFocus()
        
        time.sleep(0.5)  # 稍微等待一下,让操作生效
        text_control.Click()  # 点击以获取焦点
        
        
        # 最后定位到搜索编辑框
        search_box = first_child_control.EditControl(searchDepth=3, Name="搜索")
        print(search_box)
        # 检查搜索框是否存在
        if not search_box.Exists(3):
            print("未找到搜索框")
            return
        print("找到搜索框")
        # 设置焦点
        search_box.SetFocus()
            # 全选搜索框中的内容
        search_box.SendKeys('{Ctrl}a')
        time.sleep(0.5)  # 稍微等待一下,让操作生效
        # 删除选中的内容
        search_box.SendKeys('{Delete}')
        time.sleep(0.5)  # 稍微等待一下,让操作生效


        # 输入想要搜索的内容
        search_box.SendKeys(groupName)     
        print("搜索框的信息数据值是:")
        print(search_box)
        # 这里的第二个是父级的第二个子元素
        print("父级的第二个子元素的信息数据值是:")
        print(children[1])
        return children[1]
    listPart = getS()
    print(listPart)
    listC = listPart.ListControl(searchDepth=3)  # 按条件修改
    print("listC的信息数据值是:",listC)
    # 获取第一个子元素,可能需要根据实际的控件类型调整GetFirstChildControl的参数
    first_item = listC.GetFirstChildControl()

    # 确保第一个元素存在
    if not first_item.Exists(2, 1):
        print("未找到第一个列表项")
        return
    print("找到第一个列表项")
    
    # 例如,获取第一个元素的文本  默认是第一个完全搜索结果的群昵称
    first_item = listC.ListItemControl(foundIndex=1)

    # 对第一个元素进行操作,例如点击
    if not first_item.Exists():
        print("First item not found or not clickable")
        return
    time.sleep(0.5)  # 稍微等待一下,让操作生效
    first_item.Click()
    time.sleep(0.5)  # 稍微等待一下,让操作生效
    return  wechat_window
    
def sendImg(wechat_window,groupName,fileUrl,index=0,iterIndex=0):
    time.sleep(0.5)  # 稍微等待一下,让操作生效
    tab_control = wechat_window.ToolBarControl(searchDepth=3)
    
    if not tab_control.Exists(3, 1):
        print(f"未找到名为 '{groupName}' 的编辑控件")
        return
    # 可以正确找到群聊消息控件的数据信息
    mesgContList = wechat_window.ListControl(searchDepth=13, Name="消息")
    # print(mesgContList)
    # 打印控件信息
    # dump_tree(mesgContList)
    mgsDataList = []
    # 要搜索谁的消息 默认搜索这个群里面自己发的聊天记录,这里自己自定义。
    target_name = None
    first_child = tab_control.GetFirstChildControl()
    if first_child:
        target_name = first_child.Name
    
    max_depth = 5  # 您可以设置您希望搜索的最大深度,并且返回所有子集

    # 调用函数查找所有包含特定Name的ListItemControl控件
    matching_list_items = find_list_items_with_name(mesgContList, target_name, max_depth)

    mlistIndex = matching_list_items.__len__()
    print("上一条信息的值是",matching_list_items[mlistIndex-1].Name)
    print("当前信息的值是",index)
    if index != 0 :        
        name_str = matching_list_items[mlistIndex-1].Name
        result = None
        # 尝试将字符串转换为整数
        try:
            name_int = int(name_str)
            result = name_int + 1  # 现在可以执行减法操作
            print("改变后的值是",result)
        except ValueError:
            print(f"Cannot convert '{name_str}' to an integer.")
        if str(index).strip() == str(result).strip():
            print("上一条信息已经发送完成")
        else:
            print("上一条信息未发送完成,重新执行发送消息操作")
            if iterIndex > 3:
                print("上一条信息未发送完成,重新执行发送消息操作超过3次,退出")
                return
            return sendImg(wechat_window,groupName,fileUrl,index,iterIndex+1)

    # 打印或处理匹配的列表项
    # for list_item in matching_list_items:
    #     print(list_item.Name)  
    
    time.sleep(1)
    edit_control  = wechat_window.EditControl(searchDepth=20, Name=groupName)  # 按条件修改
    time.sleep(1)
    edit_control.SetFocus() 
    time.sleep(1)  
    edit_control.Click()
    time.sleep(1)
    # 清除当前值
    edit_control .SendKeys('{Ctrl}a{Delete}')
    # 将文件路径复制到剪贴板
    time.sleep(0.3)
    # 获取焦点
    edit_control .SetFocus()
    time.sleep(0.3)

    tmp_clipboard_image_path = os.path.join(os.getenv('TEMP'), 'clipboard_image.png')
    os.system(f'copy "{fileUrl}" "{tmp_clipboard_image_path}"')
    time.sleep(1)
    # 使用命令行工具将图片放入剪贴板
    subprocess.run(['nircmd.exe', 'clipboard', 'copyimage', tmp_clipboard_image_path])
    time.sleep(1)
    edit_control .SendKeys('{Ctrl}v')
    time.sleep(1)
    # 发送图片
    edit_control .SendKeys('{Enter}')
    time.sleep(30)
    edit_control .SendKeys('{Ctrl}a{Delete}')
    time.sleep(0.3)
    edit_control .SetFocus()
    time.sleep(1)
    edit_control .SendKeys(str(index))
    time.sleep(1)
    # 发送文字
    edit_control .SendKeys('{Enter}')
    #防止出现网络延迟
    time.sleep(1)

    #校验一下文本还有图片是否发送完成 重新获取一下消息列表
    matching_list_items = find_list_items_with_name(mesgContList, target_name, max_depth)
    mlistIndex = matching_list_items.__len__()
    print("上一条信息的值是",matching_list_items[mlistIndex-1].Name)
    print("当前信息的值是",index)
    if "[图片]" == matching_list_items[mlistIndex-1].Name:
        time.sleep(1)
        edit_control.SetFocus() 
        time.sleep(1)  
        edit_control.Click()
        time.sleep(1)
        print("上一条信息是图片,不是文本,重新执行发送文本的操作。")
        edit_control .SendKeys('{Ctrl}a{Delete}')
        time.sleep(0.3)
        edit_control .SetFocus()
        time.sleep(1)
        edit_control .SendKeys(str(index))
        time.sleep(1)
        # 发送文字
        edit_control .SendKeys('{Enter}')
        #防止出现网络延迟
        time.sleep(1)


    time.sleep(3)
    # 发送完成图片后,删除图片
    try:

        if os.path.exists(tmp_clipboard_image_path):
            os.remove(tmp_clipboard_image_path)
    except OSError as e:
        print(f"错误: {fileUrl} : {e.strerror}")
        traceback.print_exc()
        e.with_traceback()
    except Exception as e:
        print(f"错误: {fileUrl} : {e}")
        traceback.print_exc()
        e.with_traceback()
        
def find_matching_control(control, target_name, max_depth, current_depth=0):
    """递归搜索控件树,查找包含特定文本的控件"""
    if current_depth > max_depth:  # 到达最大搜索深度
        return None
    if control.Name == target_name:
        return control
    for child in control.GetChildren():
        result = find_matching_control(child, target_name, max_depth, current_depth + 1)
        if result:
            return result
    return None

def find_list_items_with_name(list_control, target_name, max_depth):
    """查找包含特定文本的所有ListItemControl"""
    matching_list_items = []
    for item in list_control.GetChildren():
        if isinstance(item, auto.ListItemControl):
            if find_matching_control(item, target_name, max_depth):
                matching_list_items.append(item)
    return matching_list_items

def sendText(wechat_window,groupName,text):
    time.sleep(1)  # 稍微等待一下,让操作生效
    tab_control = wechat_window.ToolBarControl(searchDepth=3)
    
    if not tab_control.Exists(3, 1):
        print(f"未找到名为 '{groupName}' 的编辑控件")
        return
    
    edit_control  = wechat_window.EditControl(searchDepth=20, Name=groupName)  # 按条件修改
    
    edit_control.SetFocus()   
    # 清除当前值
    edit_control .SendKeys('{Ctrl}a{Delete}')
    # 将文件路径复制到剪贴板
    
    # 获取焦点
    # edit_control .SetFocus()
    
    edit_control .SendKeys(text)
    # 发送
    edit_control .SendKeys('{Enter}')

    #防止出现网络延迟
    time.sleep(3)
    

def dump_tree(control, indent=0):
    if not control.Exists():  # 如果控件不存在则直接返回
        return
    # 打印当前控件的基本信息
    print(' ' * indent + f"ControlType: {control.ControlTypeName}, Name: {control.Name}, #{control.AutomationId}, ClassName: {control.ClassName}")
    # 对当前控件的子控件进行同样的操作
    for child in control.GetChildren():
        dump_tree(child, indent + 2)  # 增加缩进以反映层级关系

def lockW():
    # 获取微信窗口
    wechat_window = auto.WindowControl(Name="微信", ClassName="WeChatMainWndForPC")
        # 尝试逐步减小 searchDepth 的值
    for depth in range(1, 12):
        search_box = wechat_window.EditControl(searchDepth=depth, Name="搜索")
        if search_box.Exists(2):    
            print(search_box)
            print(f"成功找到消息列表控件,searchDepth={depth}")
            break
    else:
        print("在给定的层级范围内未找到消息列表控件")


#这里的图片操作我本人用的是循环读取我本地图片的。

def findFileImages():
    print("开始发送图片")
     # 获取当前脚本的完整路径(包括文件名)
    current_file_path = os.path.realpath(__file__)
    # 获取当前脚本所在的目录路径
    current_file_path = os.path.dirname(current_file_path)  
    print("当前脚本所在的目录路径:", current_file_path)
    directory_path  = current_file_path + '\\articles\\'

    now = datetime.now()
    # 将日期和时间格式化为字符串
    date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
    list = os.listdir(directory_path)
    if len(list) == 0:
        print("没有图片,等待下一次执行--",date_time_str)
        return
    print("图片数量:",len(list))
    # 读取微信群名称
    groupList = find_wx_group()
    # 微信群昵称
    for group in groupList:
        groupName = group['name']
        wechat_window = weChat(groupName)
        for index, item in enumerate(list):
            # 发送图片到微信
            id_info = os.path.splitext(item)[0]  # 移除扩展名,只保留文件名(即ID)
            print('提取的ID:', id_info)
            # curl =  directory_path +str(item['topicId'])+".png"
            # print(item)
            # curl =  directory_path + group
            curl =  directory_path +str(id_info)+".png"
            # print(curl)
            sendImg(wechat_window,groupName,curl,index)
            # 获取当前的日期和时间
        now = datetime.now()
        # 将日期和时间格式化为字符串
        date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
        text = f"{groupName}群,一共{list.__len__()}个内容,内容发送时间:{date_time_str}"
        print("图片内容发送完成--",groupName)
        sendText(wechat_window,groupName,text)
    #图片都发送完成 删除图片
    for index, item in enumerate(list):    
        fileUrl =  directory_path +str(id_info)+".png"
        if os.path.exists(fileUrl):
            os.remove(fileUrl)
    
    print("全部发送完成")

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 为了编微信自动回复脚本,我们可以使用itchat库,该库提供了对微信的接口,可以通过Python程序与微信进行交互。 以下是一个简单的微信自动回复脚本示例: ```python import itchat # 回复消息的函数 @itchat.msg_register(itchat.content.TEXT) def reply_msg(msg): if msg['FromUserName'] != myUserName: itchat.send_msg('自动回复:' + msg['Text'], msg['FromUserName']) # 登录微信 itchat.auto_login(hotReload=True) # 获取自己的用户名 myUserName = itchat.get_friends(update=True)[0]["UserName"] # 运行自动回复程序 itchat.run() ``` 这个脚本中,我们使用`itchat.msg_register()`方法注册了一个消息处理函数`reply_msg()`,当收到文本消息时,该函数会自动回复消息内容,并附加上“自动回复”前缀。同时我们使用`itchat.auto_login()`方法登录微信,然后使用`itchat.get_friends()`方法获取自己的用户名,并保存到变量`myUserName`中。最后,我们使用`itchat.run()`方法启动自动回复程序。 值得注意的是,为了使该脚本能够长时间运行,我们需要在登录时设置`hotReload=True`,这样可以让登录状态得到保存,避免重复扫码登录的情况发生。 当我们运行该脚本后,微信中的好友发送消息时,程序会自动回复消息内容,并附加上“自动回复”前缀。 ### 回答2: 使用Python微信自动回复脚本是可行的。首先,我们需要安装好Python的运行环境和相关的第三方库,如itchat。然后,通过编Python脚本,我们可以实现微信好友消息的监听和自动回复。 在脚本中,我们可以使用itchat库的API来实现微信的登录和消息的接收。通过捕获微信好友的文本消息,我们可以根据消息内容编相应的逻辑来进行回复。例如,可以设置一些关键词回复,当好友发送包含特定关键词的消息时,自动回复相应的内容。 在编脚本时,我们还可以使用正则表达式等高级技术来匹配和处理消息。例如,可以使用正则表达式来匹配特定格式的消息,并提取其中的信息用于回复。还可以利用其他Python的库来实现更复杂的逻辑,如将接收到的消息进行文本处理、分析等。 此外,我们还可以通过配置文件的方式,使脚本可以针对不同的好友或消息类型进行不同的回复。通过读取配置文件,可以灵活地设置回复规则,实现个性化的自动回复功能。 总结来说,使用Python微信自动回复脚本是一种方便且可行的方式。我们可以通过编Python脚本,利用itchat库的API和其他Python的库,实现微信好友消息的监听和自动回复,从而实现个性化和自动化微信聊天机器人。 ### 回答3: 使用Python微信自动回复脚本需要先安装一些必要的库,比如itchat,它可以模拟微信网页版的操作。 首先,在命令行中安装itchat库,可以使用pip命令:pip install itchat 然后,在Python脚本中导入itchat库:import itchat 接着,使用itchat库的login()函数登录微信账号:itchat.login() 登录成功后可以调用itchat库的函数获取微信好友列表:itchat.get_friends() 接下来,可以使用itchat库的run()函数开启微信消息监听模式,实现自动回复功能:itchat.run() 在run()函数中,可以使用装饰器@itchat.msg_register()来装饰自定义的函数,这个函数将会在收到好友消息时被调用。通过这个函数可以进行消息的处理和回复。 例如,我们可以定义一个回复消息的函数reply_to_friend(msg),其中msg是一个字典包含了收到的消息的具体信息。在函数的内部可以根据消息的类型进行逻辑判断和回复。 最后,运行整个Python脚本,扫描二维码登录微信账号后,即可实现自动回复功能。 需要注意的是,使用微信自动回复脚本有一定的限制,比如在非越狱的iPhone上,微信有防撤回机制,导致不能自动回复撤回消息。同时,使用自动回复脚本也需要谨慎,不要违反微信使用规定,以免被封号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝de笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值