wxauto4 是一个适用于微信4.0客户端的 Python 自动化库,提供微信自动化操作接口,包括消息发送、文件传输等功能。
目前仅适用于微信 4.0.5 版本客户端
下载链接:点击跳转
Warning
请勿直接点击Download URL,找到相应版本,展开Assets点击exe下载
pip install wxauto4
或者通过Github
pip install git+https://github.com/cluic/wxauto4.git
git clone https://github.com/cluic/wxauto4.git
cd wxauto4
pip install -e .
from wxauto4 import WeChat
# 创建微信实例
wx = WeChat()
# 发送消息
wx.SendMsg('你好,世界!', '好友昵称')
# 发送文件
wx.SendFiles(r'C:\path\to\file.txt', '好友昵称')
# 获取消息
messages = wx.GetAllMessage()
for msg in messages:
print(msg.content)
from wxauto4 import WeChat
# 创建微信主窗口实例
wx = WeChat()
# 基础消息发送
wx.SendMsg('Hello!', '目标用户')
参数说明:
msg
(str): 消息内容who
(str, optional): 发送对象,不指定则发送给当前聊天对象clear
(bool, optional): 发送后是否清空编辑框,默认 Trueat
(Union[str, List[str]], optional): @对象,支持字符串或列表exact
(bool, optional): 是否精确匹配用户名,默认 False
# 发送单个文件
wx.SendFiles(r'C:\path\to\file.txt', '目标用户')
# 发送多个文件
files = [
r'C:\path\to\file1.txt',
r'C:\path\to\file2.jpg',
r'C:\path\to\file3.pdf'
]
wx.SendFiles(files, '目标用户')
# 向当前聊天窗口发送文件
wx.SendFiles(r'C:\path\to\file.txt')
参数说明:
filepath
(str|list): 文件的绝对路径,支持单个文件或文件列表who
(str, optional): 发送对象,不指定则发送给当前聊天对象exact
(bool, optional): 是否精确匹配用户名,默认 False
# 获取当前聊天窗口的所有消息
all_messages = wx.GetAllMessage()
返回值:
List[Message]
: 消息列表,每个消息对象包含发送者、内容、时间、类型等信息
def on_message(msg, chat):
"""消息回调函数"""
print(f'收到来自 {chat} 的消息: {msg.content}', flush=True)
# 自动回复
if msg.content == 'hello':
chat.SendMsg('Hello! 我是xxx')
# 添加消息监听
wx.AddListenChat('好友昵称', on_message)
参数说明:
who
(str|List[str]): 监听对象,支持单个或多个callback
(Callable): 回调函数,接收(msg, chat)
两个参数
# 移除特定对象的监听
wx.RemoveListenChat('好友昵称')
# 停止所有监听
wx.StopListening()
# 切换到指定聊天窗口
wx.ChatWith('好友昵称')
参数说明:
who
(str): 要切换到的聊天对象exact
(bool, optional): 是否精确匹配名称
# 获取指定聊天的子窗口
chat_window = wx.GetSubWindow('好友昵称')
# 通过子窗口发送消息(不会切换主窗口)
chat_window.SendMsg('这是通过子窗口发送的消息')
# 获取子窗口信息
info = chat_window.ChatInfo()
print(f'聊天对象: {info["chat_name"]}')
# 关闭子窗口
chat_window.Close()
# 获取所有打开的子窗口
all_windows = wx.GetAllSubWindow()
for window in all_windows:
print(f'窗口: {window.who}')
# 可以对每个窗口进行操作
window.SendMsg('批量消息发送')
# 关闭所有子窗口
for window in all_windows:
window.Close()
# 停止所有消息监听
wx.StopListening()
# 程序结束前建议停止监听
try:
wx.SendMsg('程序即将结束', '管理员')
finally:
wx.StopListening()
from wxauto4.ui.component import UpdateWindow
if update_window := UpdateWindow():
update_window.ignore()
免责声明: 本工具仅用于学习和研究目的,使用者应当遵守相关法律法规,作者不承担任何因使用本工具而产生的法律责任。