🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝 🥰 博客首页:knighthood2001 😗 欢迎点赞👍评论🗨️ ❤️ 热爱python,期待与大家一同进步成长!!❤️ |
目录
模块导入
定义控件
搜寻聊天对象
发送图片
图片文件绝对路径获取
全部代码及结果展示
【UI自动化(uiautomation)】专栏目录介绍导航https://blog.csdn.net/knighthood2001/article/details/126319063?spm=1001.2014.3001.5502https://blog.csdn.net/knighthood2001/article/details/126319063?spm=1001.2014.3001.5502需求讲解:
在一个文件夹中,存放着许多图片,我们需要将这些图片发送给微信好友。
但是,微信提示,一次只能发送最多9个文件。所有自己手动9个9个发送会显得很呆。
因此,我们需要写个自动化脚本,来代替人工发送图片。
但是这里笔者就不一次发送9个文件。
实现过程如下:
【UI自动化】通过剪切板发送文本链接
一些内容可参考之前写的发送文本的文章
【UI自动化】通过剪切板发送文本
import os
import time
import uiautomation as auto
from uiautomation import Bitmap
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}')elif message_type == 3:# 图片类型auto.SetClipboardBitmap(Bitmap.FromFile(content))edit.SendKeys('{Ctrl}v')# 最后回车,发送信息auto.SendKeys('{Enter}')
这里笔者加入了前两种情况,这里主要看第三种情况。
elif message_type == 3:# 图片类型auto.SetClipboardBitmap(Bitmap.FromFile(content))edit.SendKeys('{Ctrl}v')# 最后回车,发送信息auto.SendKeys('{Enter}')
def SetClipboardBitmap(bitmap: Bitmap) -> bool:
SetClipboardBitmap(bitmap: Bitmap),该函数直接翻译就是设置位图到剪切板,其中输入是bitmap,输出为bool类型。
def FromFile(filePath: str) -> 'Bitmap':"""Create a `Bitmap` from a file path.filePath: str.Return `Bitmap` or None."""bitmap = Bitmap()bitmap._bitmap = _DllClient.instance().dll.BitmapFromFile(ctypes.c_wchar_p(filePath))if bitmap._bitmap:bitmap._getsize()return bitmap
FromFile()从filePath(文件路径)创建一个“位图”。
返回' Bitmap '或None。
将其弄到剪切板中,我们再粘贴一下就好了。
另外,对于官方库中,
def GetClipboardBitmap() -> Bitmap:
给出了GetClipboardBitmap() 函数,但是其返回的是位图
也就是这里的
因此笔者没有找到使用该函数实现粘贴功能的。
python获取文件夹下所有图片目录
对于给出的四种方法,笔者采用法二(看着少,好理解与更改)
def getfiles(file):filenames = os.listdir(file)for i in range(len(filenames)):filenames[i] = r"{}\{}".format(file, filenames[i])# print(filenames[i])# print(filenames)return filenames
import os
import time
import uiautomation as auto
from uiautomation import BitmapwechatWindow = 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}')elif message_type == 3:# 图片类型auto.SetClipboardBitmap(Bitmap.FromFile(content))edit.SendKeys('{Ctrl}v')# 最后回车,发送信息auto.SendKeys('{Enter}')def getfiles(file):filenames = os.listdir(file)for i in range(len(filenames)):filenames[i] = r"{}\{}".format(file, filenames[i])# print(filenames[i])# print(filenames)return filenamesif __name__ == '__main__':# TODO 微信好友备注name = "小号"search_object(name)text = '我在浙江很想你'# 1 文本send_message(text, message_type=1)# 2 拆分文本send_message(text, message_type=2)# 3 发一张图片file_along = r'C:\Users\knighthood\OneDrive\桌面\熊二表情包\熊二表情包41.jpg'send_message(file_along, message_type=3)# 4 批量发图片file = r'C:\Users\knighthood\OneDrive\桌面\熊二表情包'filenames = getfiles(file)# # 交互差一点# for filename in filenames:# send_message(filename, message_type=3)# print(filename, '发送')# 交互好一点num = len(filenames)for index, filename in enumerate(filenames):send_message(filename, message_type=3)print('[{}/{}]'.format(index+1, num), filename, '发送')
批量发图片方面,交互差一点的,只显示发送,未显示这是第几张图片。
交互好一点的,前面出现了第几张/总图片张数
如果觉得笔者写的不错的话,欢迎订阅专栏,笔者会努力更新这方面的内容。