• 【UI自动化】通过剪切板发送文本


    🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝 

    🥰 博客首页:knighthood2001

    😗 欢迎点赞👍评论🗨️

    ❤️ 热爱python,期待与大家一同进步成长!!❤️

    最受欢迎专栏:UI自动化(uiautomation)

    目录

    模块导入

    定义控件

    搜寻聊天对象

    发送文本

    发送拆分文本

    全部代码展示

    结尾


    需求:通过剪切板实现将文本内容粘贴,实现发送。

    实现过程如下


    模块导入

    1. import time
    2. import uiautomation as auto

    定义控件

    1. wechatWindow = auto.WindowControl(searchDepth=1, Name="微信", ClassName='WeChatMainWndForPC')
    2. # 有前提条件
    3. wechatWindow.SetActive()
    4. search = wechatWindow.EditControl(Name='搜索')
    5. edit = wechatWindow.EditControl(Name='输入')
    6. messages = wechatWindow.ListControl(Name='消息')

    这里注意一下

    # 有前提条件
    wechatWindow.SetActive()

    这段代码有前提条件,首先微信需要处在激活状态,然后才能将顶层窗口设置为活动状态

    微信需要处在打开状态(而不是在最右边的)


    搜寻聊天对象

    1. def search_object(name, wait_time=0.1):
    2. search.Click()
    3. # auto.SetClipboardText(name)
    4. # edit.SendKeys('{Ctrl}v')
    5. # 也可以使用下面的
    6. auto.SendKeys(name)
    7. # 等待一会,防止出错
    8. time.sleep(wait_time)
    9. search.SendKeys("{Enter}")

    如上,输入聊天对象名称可以使用两种方法,一种

    auto.SendKeys(name)

    另一种则通过

    1. auto.SetClipboardText(name)
    2. edit.SendKeys('{Ctrl}v')

    复制内容到剪贴板,然后输入ctrl+v,粘贴。

    发送文本

    1. def send_message(content, message_type=1):
    2. if message_type == 1:
    3. # 文本类型
    4. auto.SetClipboardText(content)
    5. # 粘贴
    6. edit.SendKeys('{Ctrl}v')
    7. # 发送消息
    8. auto.SendKeys('{Enter}')

    先定义一个函数,content参数为文本内容,message_type是发送消息类型。

    auto.SetClipboardText(content)

    该段代码表示,将文本复制到剪贴板

    edit.SendKeys('{Ctrl}v')

    接下来使用粘贴组合键即可将内容粘贴到聊天框。

    粘贴文本功能你还可以使用下面的方法

    edit.SendKeys(auto.GetClipboardText())
    • auto.GetClipboardText()表示取剪辑板文本
    • 这段代码表示输入从剪切板中获取到的文本

    发送拆分文本

    1. elif message_type == 2:
    2. # 文本拆分发送
    3. text_split = list(content)
    4. for i in text_split:
    5. auto.SetClipboardText(i)
    6. edit.SendKeys('{Ctrl}v')
    7. # 发送消息
    8. auto.SendKeys('{Enter}')

    message_type文本发送类型为2时,通过将我输入的整段文字拆分开,

    content = '我在浙江很想你'

    拆分成   ['我', '在', '浙', '江', '很', '想', '你'],然后遍历列表,依次发送。

    将上述两个部分结合在一起,如下

    1. def send_message(content, message_type=1):
    2. if message_type == 1:
    3. # 文本类型
    4. auto.SetClipboardText(content)
    5. # 粘贴
    6. edit.SendKeys(auto.GetClipboardText())
    7. # 发送消息
    8. auto.SendKeys('{Enter}')
    9. elif message_type == 2:
    10. # 文本拆分发送
    11. text_split = list(content)
    12. print(text_split)
    13. for i in text_split:
    14. auto.SetClipboardText(i)
    15. edit.SendKeys('{Ctrl}v')
    16. # 发送消息
    17. auto.SendKeys('{Enter}')

    全部代码展示

    1. import time
    2. import uiautomation as auto
    3. wechatWindow = auto.WindowControl(searchDepth=1, Name="微信", ClassName='WeChatMainWndForPC')
    4. # 有前提条件
    5. wechatWindow.SetActive()
    6. search = wechatWindow.EditControl(Name='搜索')
    7. edit = wechatWindow.EditControl(Name='输入')
    8. messages = wechatWindow.ListControl(Name='消息')
    9. def search_object(name, wait_time=0.1):
    10. search.Click()
    11. # auto.SetClipboardText(name)
    12. # edit.SendKeys('{Ctrl}v')
    13. # 也可以使用下面的
    14. auto.SendKeys(name)
    15. # 等待一会,防止出错
    16. time.sleep(wait_time)
    17. search.SendKeys("{Enter}")
    18. def send_message(content, message_type=1):
    19. if message_type == 1:
    20. # 文本类型
    21. auto.SetClipboardText(content)
    22. # 粘贴
    23. edit.SendKeys(auto.GetClipboardText())
    24. # 发送消息
    25. auto.SendKeys('{Enter}')
    26. elif message_type == 2:
    27. # 文本拆分发送
    28. text_split = list(content)
    29. print(text_split)
    30. for i in text_split:
    31. auto.SetClipboardText(i)
    32. edit.SendKeys('{Ctrl}v')
    33. # 发送消息
    34. auto.SendKeys('{Enter}')
    35. if __name__ == '__main__':
    36. name = "小号"
    37. search_object(name)
    38. text = '我在浙江很想你'
    39. send_message(text, message_type=1)
    40. send_message(text, message_type=2)

    运行结果如下 

    结尾

            通过剪切板发送文本就介绍到这里了。后续更新批量发送图片。

  • 相关阅读:
    AUTOCAD——Excel表格导入CAD、CAD合并两兄弟
    TCP/IP 网络编程(二):TCP原理
    中级软件设计师考试(软考中级)计算机专业英语
    会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核
    uniapp/微信小程序 项目day03
    python读取图片
    前端周刊第三十二期
    Linux云服务环境安装-Nginx篇
    实战|基于YOLOv10与MobileSAM实现目标检测与分割【附完整源码】
    Linux操作系统之进程控制
  • 原文地址:https://blog.csdn.net/knighthood2001/article/details/127168109