需求
为了能将摄像头的画面自动发送到微信群里,查找发现基于这个博客的代码修改后可是使用,这里记录一下
Python自动化实现微信自动发送文件_python自动发送微信文件-CSDN博客
实现前的准备
- pycharm+python(我使用的anaconda创建的3.8版本)
- 我的摄像头是小米摄像头,在电脑上用要下了一个安卓模拟器(我用的雷电模拟器),安装一个米家软件,并能正常使用
- 登录好微信
- 排查故障用的软件,路径查找工具,我用的windows SDK自带的inspect。(我用到了,和博主的路径不一样,代码做了修改)
- pyperclip、time、wxauto、uiautomation,复制代码后哪些包没有有提示
实现
代码截图的原理
# -*- coding: utf-8 -*-
import time
import pyperclip # 导入pyperclip库
import os
import pyautogui
from wxauto import *
from uiautomation import WindowControl, PaneControl
def gettimestr():
t = time.time()
ms = int(round(((t - int(t)) * 1000)))
timestr = time.strftime('[%H:%M:%S.', time.localtime()) + '{:0>3}'.format(ms) + ']'
return timestr
send_cnt = 0
while True:
try:
# 打开隐藏的图标
PaneControl(Name='任务栏').PaneControl(ClassName='TrayNotifyWnd').ButtonControl(Name='通知 V 形').Click()
# 点击微信
PaneControl(Name='通知溢出').ButtonControl(Name='雷电模拟器').Click()
pyautogui.hotkey('ctrl', '0') # 使用快捷键截图
time.sleep(5)
# 输入群聊名称
names = ['测试']
# 输入文件名和路径
# file_name = 'Screenshot_20241106-172345.png'
file_path = r'C:\Users\xtk\Documents\leidian9\Pictures\Screenshots'
# 查找最新的截图文件名
file_lists = os.listdir(file_path)
file_lists.sort(key=lambda fn: os.path.getmtime(file_path + "\\" + fn))
file_name = file_lists[-1]
time.sleep(1)
# 打开隐藏的图标
# PaneControl(Name='任务栏').PaneControl(ClassName='TrayNotifyWnd').ButtonControl(Name='通知 V 形').Click()
# 点击微信
PaneControl(Name='通知溢出').ButtonControl(Name='微信').Click()
# 绑定微信主窗口
wx = WindowControl(Name='微信')
# 选择群聊
for name in names:
WeChat().ChatWith(name)
# 点击发送文件
wx.ButtonControl(Name='发送文件').Click()
# 发送文件
# 输入文件地址
pyperclip.copy(file_path) # 使用pyperclip将文件路径复制到剪贴板
pyautogui.hotkey('ctrl', 'v') # 使用快捷键粘贴文件路径
time.sleep(1)
pyautogui.press('enter')
# 输入文件名
pyperclip.copy(file_name) # 使用pyperclip将文件名复制到剪贴板
pyautogui.hotkey('ctrl', 'v') # 使用快捷键粘贴文件名
time.sleep(1)
pyautogui.press('enter')
# 发送
pyautogui.press('enter')
send_cnt += 1
print('已完成 ', send_cnt, ' 次', " 时间:", gettimestr())
except LookupError:
print('操作中断 ', "时间:", gettimestr())
time.sleep(3600 * 6)
还是比较简单,主要是修改查询路径可能会报错