Skip to content

基础用法

初始化

python
from pywxauto import Weixin

# 最简初始化(自动连接已登录的微信)
wx = Weixin()

# 指定进程 PID
wx = Weixin(pid=12345)

# 后台模式(不需要窗口前台显示)
wx = Weixin(background=True)

发送消息

发送文本

python
wx.send_text("张三", "你好!")

# 带引用回复
chat = wx.chat_with("张三")
messages = chat.get_visible_messages()
wx.send_text("张三", "收到了", quote=messages[-1])

发送文件/图片/视频

python
# 发送文件
wx.send_file("张三", "C:/docs/report.pdf")

# 发送多个文件
wx.send_file("张三", ["C:/a.pdf", "C:/b.pdf"])

# 发送图片
wx.send_image("张三", "C:/photos/pic.png")

# 发送视频
wx.send_video("张三", "C:/videos/demo.mp4")

# 支持 URL(自动下载后发送)
wx.send_image("张三", "https://example.com/image.png")

发送表情

python
# 搜索表情并发送
wx.send_emotion("张三", keyword="哈喽")

# 发送自定义表情(第N个)
wx.send_emotion("张三", index=1)

发送收藏内容

python
wx.send_collection("张三", "重要文档")

@群成员

python
wx.send_at("工作群", "开会了", at_members=["张三", "李四"])

# @所有人
wx.send_at("工作群", "紧急通知", at_members=["所有人"])

发送名片

python
# 将张三的名片发送给李四
wx.send_card("李四", "张三")

会话管理

打开会话

python
# 通过搜索打开
chat = wx.open_session_by_search("张三")

# 强制搜索(跳过列表直接搜索)
chat = wx.open_session_by_search("张三", force_search=True)

# 获取当前激活的聊天
chat = wx.chat

会话列表

python
session = wx.session

# 获取可见会话列表
items = session.visible()

# 遍历所有会话(通过滚动)
for item in session.iter_sessions_by_scroll(count=50):
    print(f"{item.name} - 未读: {item.unread_count}")

会话操作

python
# 置顶/取消置顶
session.pin("张三")
session.unpin("张三")

# 消息免打扰
session.mute("工作群")
session.unmute("工作群")

# 标为未读
session.mark_as_unread("张三")

# 删除会话
session.delete("张三")

读取消息

python
chat = wx.chat_with("张三")

# 获取当前可见消息
messages = chat.get_visible_messages()
for msg in messages:
    print(f"[{msg.type_label}] {msg.sender}: {msg.content}")

# 遍历历史消息(从最新到最旧)
for msg in chat.iter_recently_messages(count=100):
    print(msg)

消息操作

python
msg = messages[-1]

# 引用
msg.quote()

# 复制
text = msg.copy()

# 转发
msg.forward(["李四", "王五"], remark="转发给你看看")

# 收藏
msg.collect()

# 撤回(仅限自己发的消息,2分钟内)
msg.revoke()

# 删除
msg.delete()

群聊管理

python
chat = wx.chat_with("工作群")

# 群信息
print(chat.chat_name)      # 群名
print(chat.member_count)   # 成员数

# 设置群信息
chat.set_room_info(
    name="新群名",
    announcement="群公告内容",
    my_nickname="我的群昵称",
    mute=True,
    pin=True,
)

# 添加/移除成员
chat.add_room_members(["新成员1", "新成员2"])
chat.remove_room_members(["要移除的人"])

# 退出群聊
chat.exit_room()

联系人管理

python
# 获取联系人资料
profile = wx.get_contact_profile("张三")
print(profile)

# 设置备注
wx.set_contact_remark("张三", "同事-张")

# 设置标签
wx.set_contact_label("张三", ["同事", "项目组"])

# 设置星标
wx.set_contact_star("张三")

# 加入黑名单
wx.black_contact("骚扰者")

# 删除联系人
wx.delete_contact("前同事")

添加好友

python
result = wx.add_friend(
    "wxid_xxx",
    message="你好,我是XXX",
    remark="同事",
)
print(result)  # {"result": True, "reason": None}

发起群聊

python
wx.create_room(["张三", "李四", "王五"])

状态检查

python
# 微信状态
print(wx.status)       # "online" / "locked" / "login" / "offline"
print(wx.is_online)    # True/False
print(wx.is_locked)    # True/False

# 新消息检查
print(wx.check_new_messages())   # 有新消息?
print(wx.check_new_contacts())   # 有新联系人请求?
print(wx.check_new_moments())    # 有新朋友圈通知?