具体流程就是:
1. 打开微信
2. 点击通讯录
3. 滚动鼠标到最顶部(防止已经滚动了一部分了)
4. 获取联系人列表
5. 找到最后一个空格所在的位置(后一个就是真正的联系人了)
6. 点击第一个联系人
7.记录下上一个联系人的微信号
7. 无限循环按键盘下箭头
当前微信号与上一个相同的时候,说明到底部了,可以跳出循环
获取详情中的以下元素内容
"微信号", "昵称", "地区", "备注", "标签", "签名", "来源"
逐行写入excel
效果图:
详细代码:
import datetime
import uiautomation as uia
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Font, PatternFill
# 获取所有的会话列表
# 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)