Python【代码 01】导出微信通讯录(微信号+昵称+地区+备注+标签+签名+来源)操作说明(可执行文件exe分享)

感谢程序员老狼的博文《【分享】获取微信通讯录python代码形式实现》的代码分享,这里算是操作说明。还使用 Pyinstaller简单打了个 .exe的包,网盘分享压缩包文件 exportWechatContacts.zip 大小 234MB

通过网盘分享的文件:CSDN_Python_exportWechatContacts

链接: https://pan.baidu.com/s/1WrFF8I_j_PumlvQNE8J0kg?pwd=quju 提取码: quju

1.环境

# python -V
Python 3.9.18
# 类库安装我使用的是conda
conda install uiautomation==2.0.18

2.代码

创建 Python 文件 exportWechatContacts.py粘贴以下代码:

import datetime

import uiautomation as uia
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Font, PatternFill


def main():
    # 获取所有的会话列表
    # pip install openpyxl pandas uiautomation
    # 初始化微信窗口控件
    wechat_window = uia.WindowControl(ClassName='WeChatMainWndForPC')
    wechat_window.SwitchToThisWindow()
    wechat_window.MoveToCenter()
    # 获取窗口的坐标和尺寸
    window_rect = wechat_window.BoundingRectangle
    window_left, window_top, window_width, window_height = window_rect.left, window_rect.top, window_rect.width(), window_rect.height()
    print("微信窗口坐标:", window_left, window_top)
    print("微信窗口宽度:", window_width)
    print("微信窗口高度:", window_height)

    toolBar = wechat_window.ToolBarControl(Name="导航")
    # 用于存储获取到的昵称和微信号名称
    contacts = []

    toolBar.GetChildren()[2].Click()

    # 滚动到顶部
    prevTop = ""
    sameTopCount = 0
    while sameTopCount < 2:
        session_list = wechat_window.ListControl(Name='联系人')
        currentTop = session_list.GetChildren()[0].Name

        if currentTop == prevTop:
            sameTopCount += 1
        else:
            sameTopCount = 0
        prevTop = currentTop
        session_list.WheelUp(wheelTimes=20, waitTime=0.1)

    # 循环通讯录
    # 记录上一次微信号
    preWechatCode = ""
    # 重新获取会话列表控件
    session_list = wechat_window.ListControl(Name='联系人').GetChildren()
    # 从后往前找空格
    index = len(session_list)
    for index, item in reversed(list(enumerate(session_list))):
        if item.Name == "":
            break
    # 第一个联系人点击
    session_list[index + 1].Click()
    # 获取当前时间
    current_time = datetime.datetime.now().strftime("%Y-%m-%d")
    filename = f"通讯录{current_time}.xlsx"
    # 创建新的工作簿和工作表
    wb = Workbook()
    ws = wb.active
    # 写入标题行
    headers = ["code", "nickname", "area", "remark", "tag", "sign", "from"]
    headersName = ["微信号", "昵称", "地区", "备注", "标签", "签名", "来源"]
    # 设置字体颜色为白色,背景色为蓝色
    font_color = Font(color="FFFFFF")
    fill_color = PatternFill(start_color="0000FF", end_color="0000FF", fill_type="solid")

    for col_num, header in enumerate(headersName, 1):
        cell = ws.cell(row=1, column=col_num, value=header)
        cell.font = font_color
        cell.fill = fill_color
    wb.save(filename)

    while True:
        try:

            wechatCodeTag = wechat_window.TextControl(Name="微信号:")
            if not wechatCodeTag.Exists(0.1):
                wechat_window.SendKeys("{DOWN}")
                continue
            contact = {"code": wechatCodeTag.GetNextSiblingControl().Name, "nickname": "", "area": "", "remark": "",
                       "tag": "", "sign": "", "from": ""}
            # 到底部
            if preWechatCode == contact["code"]:
                break
            preWechatCode = contact["code"]
            contact["nickname"] = wechat_window.ButtonControl(Name="更多").GetPreviousSiblingControl().Name
            nicknameTag = wechat_window.TextControl(Name="昵称:")
            if nicknameTag.Exists(0.1):
                contact["remark"] = contact["nickname"]
                contact["nickname"] = nicknameTag.GetNextSiblingControl().Name

            areaTag = wechat_window.TextControl(Name="地区:")
            if areaTag.Exists(0.1):
                contact["area"] = areaTag.GetNextSiblingControl().Name

            signTag = wechat_window.TextControl(Name="个性签名")
            if signTag.Exists(0.1):
                contact["sign"] = signTag.GetNextSiblingControl().Name

            tagTag = wechat_window.TextControl(Name="标签")
            if tagTag.Exists(0.1):
                contact["tag"] = tagTag.GetNextSiblingControl().Name

            fromTag = wechat_window.TextControl(Name="来源")
            if fromTag.Exists(0.1):
                contact["from"] = fromTag.GetNextSiblingControl().Name

            print(contact)
            # 加载当前的Excel文件
            wb = load_workbook(filename)
            ws = wb.active

            # 追加数据
            row = [contact[key] for key in headers]
            ws.append(row)
            # 保存到Excel文件
            wb.save(filename)
            print(f"数据已写入{filename}")

            wechat_window.SendKeys("{DOWN}")
        except Exception as e:
            print(e)


if __name__ == '__main__':
    main()

3.执行

打开微信,使用 python 来执行文件 exportWechatContacts.py

此时微信窗口会置顶,开始扫描通讯录,从群聊开始,直到第一个联系人,黑窗口会输出日志信息:

通讯录会一直循环,同时生成名称为 通讯录+年月日.xlsx的文件,内容如下:

4.exe 程序

如果没有Python环境,可直接下载压缩包,解压后会有一个目录一个可执行文件:

双击脚本exportWechatContacts.ext即可导出通讯录:

5.注意

  1. 可随时中断;
  2. 执行的次数过多,微信可能会检测到风险,自动退出登录。
评论 43
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanzhengme.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值