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

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

🥰 博客首页:knighthood2001

😗 欢迎点赞👍评论🗨️

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

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

目录

模块导入

定义控件

搜寻聊天对象

发送文本

发送拆分文本

全部代码展示

结尾


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

实现过程如下


模块导入

import time
import uiautomation as auto

定义控件

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

这里注意一下

# 有前提条件
wechatWindow.SetActive()

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

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


搜寻聊天对象

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

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

auto.SendKeys(name)

另一种则通过

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

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

发送文本

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

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

auto.SetClipboardText(content)

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

edit.SendKeys('{Ctrl}v')

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

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

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

发送拆分文本

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

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

content = '我在浙江很想你'

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

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

def send_message(content, message_type=1):
    if message_type == 1:
        # 文本类型
        auto.SetClipboardText(content)
        # 粘贴
        edit.SendKeys(auto.GetClipboardText())
        # 发送消息
        auto.SendKeys('{Enter}')
    elif message_type == 2:
        # 文本拆分发送
        text_split = list(content)
        print(text_split)
        for i in text_split:
            auto.SetClipboardText(i)
            edit.SendKeys('{Ctrl}v')
            # 发送消息
            auto.SendKeys('{Enter}')

全部代码展示

import time
import uiautomation as auto

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

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

def send_message(content, message_type=1):
    if message_type == 1:
        # 文本类型
        auto.SetClipboardText(content)
        # 粘贴
        edit.SendKeys(auto.GetClipboardText())
        # 发送消息
        auto.SendKeys('{Enter}')
    elif message_type == 2:
        # 文本拆分发送
        text_split = list(content)
        print(text_split)
        for i in text_split:
            auto.SetClipboardText(i)
            edit.SendKeys('{Ctrl}v')
            # 发送消息
            auto.SendKeys('{Enter}')


if __name__ == '__main__':
    name = "小号"
    search_object(name)
    text = '我在浙江很想你'
    send_message(text, message_type=1)
    send_message(text, message_type=2)

运行结果如下 

结尾

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

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

knighthood2001

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

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

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

打赏作者

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

抵扣说明:

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

余额充值