Skip to main content

wx4py

Python 微信自动化工具 — 基于 UIAutomation 的 Windows Qt 版微信桌面客户端自动化库。

功能

模块 功能
消息发送 向联系人或群聊发送文本消息、文件,支持批量发送
消息监听 实时监听群聊消息,基于轮询模式的稳定监听器
自动回复 处理器管线(Handler/Action),支持多 handler 组合
消息转发 跨群转发文本/图片/卡片消息,含转发规则引擎
AI 集成 内置 OpenAI 兼容接口的 AI 客户端,支持流式回复
群组管理 获取群列表、搜索群聊、管理群成员
剪贴板 Markdown → HTML → 剪贴板,支持富文本发送
窗口控制 自动连接微信窗口、注册表修复、托盘恢复

安装

pip install wxpy4

系统要求: Windows 10+,Qt 版微信客户端(WeChat.exe)。

快速开始

from wx4py import WeChatClient

# 连接微信
wx = WeChatClient()
wx.connect()

# 发送消息给联系人
wx.chat_window.send_to("张三", "你好!")

# 发送文件到群聊
wx.chat_window.send_file_to("测试群", "report.xlsx", target_type="group")

# 批量发送
wx.chat_window.batch_send(["群1", "群2"], "通知:明天 9 点开会")

# 使用上下文管理器(自动 disconnect)
with WeChatClient() as wx:
    wx.chat_window.send_to("张三", "Hello from context manager")

群消息监听与自动回复

from wx4py import WeChatClient, MessageHandler, ReplyAction

class EchoHandler(MessageHandler):
    def handle(self, msg):
        if "你好" in msg.text:
            return ReplyAction(f"你好 {msg.sender}!")
        return None

wx = WeChatClient()
wx.connect()
processor = wx.process_groups(["测试群"], EchoHandler())
# processor.run_forever()  # 阻塞运行
processor.stop()  # 或按需停止

消息转发

from wx4py import WeChatClient, GroupForwardRule, ForwardRuleHandler

rule = GroupForwardRule(
    source_groups=["源群A"],
    target_groups=["目标群B"],
    text_only=True,
)
handler = ForwardRuleHandler(rule)

wx = WeChatClient()
wx.connect()
processor = wx.process_groups(["源群A"], handler)

AI 自动回复

from wx4py import WeChatClient, AIResponder, AIConfig

config = AIConfig(
    base_url="https://api.openai.com/v1",
    api_key="sk-xxx",
    model="gpt-4o",
)
responder = AIResponder(config, system_prompt="你是一个友好的助手")

wx = WeChatClient()
wx.connect()
processor = wx.process_groups(["客服群"], responder.to_handler())

API 概览

核心类

类 说明
WeChatClient 主入口:连接微信、获取聊天窗口和群管理器
ChatWindow 聊天窗口:发送消息、文件、搜索
GroupManager 群管理器:获取群列表、搜索群
WeChatGroupListener 群消息监听器:轮询新消息
WeChatGroupProcessor 消息处理器:管理监听生命周期

消息处理

类 说明
MessageHandler 抽象处理器基类
CallbackHandler 回调式处理器(函数式风格)
AsyncCallbackHandler 异步回调处理器
ReplyAction 回复动作
ForwardAction 转发动作

消息转发

类 说明
GroupForwardRule 转发规则定义
ForwardRuleHandler 转发规则处理器
ForwardTarget 转发目标
ForwardPayload 转发载荷

AI 集成

类 说明
AIClient OpenAI 兼容 API 客户端
AIConfig AI 配置(base_url, api_key, model)
AIResponder AI 自动回复器
MessageEvent 消息事件数据类

技术栈

  • UIAutomation: Windows UI 自动化框架
  • COM (comtypes): Windows 组件对象模型交互
  • pywin32: Windows API 调用
  • BeautifulSoup: HTML 解析(聊天内容解析)
  • Pillow: 图片处理
  • Markdown: Markdown → HTML 转换

注意事项

  1. 仅支持 Windows — 依赖 UIAutomation、pywin32、comtypes 等 Windows 专用库
  2. 仅支持 Qt 版微信 — 不支持 UWP 版(Microsoft Store 版本)
  3. 微信需要保持前台运行 — 自动化通过 UI 操作实现,微信窗口不能被最小化到系统托盘
  4. 首次使用需要管理员权限 — 用于注册 COM 组件
  5. Python 3.8+ — 使用了 from __future__ import annotations

License

MIT

Metadata

Release files for wxpy4 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for wxpy4 0.5.1
File Interpreter ABI Platform Actions
wxpy4-0.5.1-py3-none-any.whl Python 3 none any Details for wxpy4-0.5.1-py3-none-any.whl

Release files / wxpy4-0.5.1-py3-none-any.whl

Download URL wxpy4-0.5.1-py3-none-any.whl
Size 132.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6438d9eb24f110dcd2c91e45527f4276b9d7bc35d5849e329a5d91613620037
BLAKE2b-256 checksum
How to use checksums
75f5180d903d2c69765a78da1d900447ee819a9336d8ac52a0807fb0b78cc43f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for wxpy4 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page