吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6855|回复: 27
收起左侧

[Python 转载] 微信文件自动分类整理

  [复制链接]
youpc529 发表于 2021-6-30 14:54
一、需求和问题:
日积月累,微信接收的文件非常多非常乱,现要按照不同的发送人和不同的群分类整理文件,但是微信默认的是按月份分类整理,如下:
微信截图_20210630142719.png
二、思路
虽然接收的文件夹没有按照不同发送人和群分类整理,但是微信客户端还是提供了筛选的功能,如下:
微信截图_20210630143602.png
1、利用筛选功能获取所有的文件和该文件来源于哪个发送人或群的信息,信息以字典方式存储——{文件名称:文件来源};
2、根据存储的发送人和群的信息,即根据文件来源创建对应文件夹;
3、遍历微信接收文件夹中的所有文件,结合存储的数据,将文件拷贝到新创建的相应的文件夹中。
三、工具
1、电脑客户端自动化——pywinauto;
2、窗口控件信息获取——winspect。
四、具体代码
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from pywinauto import Desktop, Application
import pyautogui
import re
import os
import shutil
 
data_dic={}
folder_name=[]
file_name=[]
 
#启动微信
os.environ.update({"__COMPAT_LAYER":"RUnAsInvoker"})
app = Application(backend='uia').start(r'D:\Program Files (x86)\Tencent\WeChat\WeChat.exe')
 
#进入微信文件筛选界面
win_main_Dialog = Desktop(backend='uia').window(title='微信测试版')
wj=win_main_Dialog.window(title='微信文件')
wj.click_input()
win_main_Dialog = Desktop(backend='uia').window(title='微信文件')
wj2=win_main_Dialog.child_window(title='全部')
wj2.click_input()
 
#7条信息一个循环
def press_down():
    for i in range(7):
        pyautogui.press('down')
#读取数据函数  
def read_data():
    str_n='te'
    list_now=[]
    for i in range(1,8):
        #使用inspect探测元素定位,提取文件名
        fi_n=win_main_Dialog.children()[1].children()[2].children()[1].children()[1].children()[0].children()[0].children()[0].children()[0].children()[i].children()[0].children()[0].children()[1].children()[0].children()[0].children()[0].children()[0].window_text()
        #使用inspect探测元素定位,提取文件夹名,区分个人和群
        fo_n=win_main_Dialog.children()[1].children()[2].children()[1].children()[1].children()[0].children()[0].children()[0].children()[0].children()[i].children()[0].children()[0].children()[1].children()[0].children()[1].children()[0].window_text()
        if fo_n[-3:] == '的聊天':
            fo_n=re.findall('与(.*)的聊天',fo_n)[0]
        else:
            number=re.search(u"\|(.*?)",fo_n).span()[1]+1
            fo_n=fo_n[number:]
        file_name.append(fi_n.strip())
        folder_name.append(fo_n.strip())
        #无法统计接收文件数量,滚动条下拉到底即读取结束,判断依据为前后7条截取的部分信息一致
        list_now.append(fi_n.strip()[:2])
        str_n+=fi_n.strip()[:2]
    return list_now,str_n
 
#数据提取
for i in range(5):
    wj2.type_keys("{TAB}")#切换到文件信息页面
str_b=read_data()#读取第一页
press_down()#两次向下点击,进入第二页
press_down()#两次向下点击,进入第二页
pyautogui.press('down')
str_n=read_data()#读取第二页
press_down()#向下点击,进入第二页
#循环提取直到结束
while str_b!=str_n:
    str_b=str_n
    str_n=read_data()
    press_down()
 
#数据存储
for i in range(len(file_name)):
    if file_name[i] in data_dic:
        data_dic[file_name[i]]+=[folder_name[i]]
    else:
        data_dic[file_name[i]]=[folder_name[i]]
 
#根据提取的个人或群的文件夹名称,在迁移目的地文件夹创建对应文件夹,以便将相应文件存入
folder_path=r'd:/微信整理文件夹'
for i in set(folder_name):
    try:
        os.makedirs(os.path.join(folder_path,i))#创建分类文件夹
    except OSError:
        print(i)
         
#遍历所有微信接收文件,将文件拷贝到整理后的指定的文件夹中
for root,dirs,files in os.walk(r'D:\WeChat Files\wxid_mpxkxr21\FileStorage\File'):
    for file in files:
        old_path=os.path.join(root,file)#原始路径
        #文件名称调整,重复的文件,文件名后有多个'(1)'
        f_name=os.path.splitext(file)[0]
        f_name_s=os.path.splitext(file)[1]
        while f_name[-3:]=='(1)':
            f_name=f_name[0:-3]
             
        try:
            des_path_list=data_dic[f_name+f_name_s]
            for j in des_path_list:
                des_path=os.path.join(folder_path,j,file)#新的文件路径
                shutil.copyfile(old_path,des_path)
        except KeyError:
            print('对应文件不存在')
            print(os.path.join(root,file))
        except OSError:
            print('文件夹名称不符合规范')
            print(os.path.join(root,file))




免费评分

参与人数 8吾爱币 +12 热心值 +5 收起 理由
zhu1979 + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
lnxceo + 1 + 1 我很赞同!
上帝的妞 + 1 + 1 热心回复!
MIC生命互联网 + 1 已经处理,感谢您对吾爱破解论坛的支持!
qinluezhew3 + 1 热心回复!
艾sk贝拉 + 1 + 1 我很赞同!
诗和远方代言人 + 1 + 1 建议分享python的都生成一份exe执行文件最好!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xuejiqiao 发表于 2021-6-30 15:16
有具体的使用方法吗,这个真的是太棒了
brux 发表于 2021-8-18 13:18
Traceback (most recent call last):
  File "D:\MF\WechatFIlesOrganized.py", line 53, in <module>
    str_b=read_data()#读取第一页
  File "D:\MF\WechatFIlesOrganized.py", line 35, in read_data
    fi_n=win_main_Dialog.children()[1].children()[2].children()[1].children()[1].children()[0].children()[0].children()[0].children()[0].children()[i].children()[0].children()[0].children()[1].children()[0].children()[0].children()[0].children()[0].window_text()
IndexError: list index out of range
zhb7642 发表于 2021-6-30 15:11
yiqisese 发表于 2021-6-30 15:21
这个好用,咋用呢
hf2009 发表于 2021-6-30 15:43
怎么用呢?期待中……
艾sk贝拉 发表于 2021-6-30 15:45
赞一个, 如果能搞成傻瓜版就好了
我爱你若能倒着 发表于 2021-6-30 15:49
这个挺实用的 不然有时候想删东西 又怕删了有用的
龍謹 发表于 2021-6-30 15:57
谢谢分享PY知识。
wuai920981023 发表于 2021-6-30 16:24
使用教程?????????
xilidexiao 发表于 2021-6-30 16:32
pywinauto.findwindows.ElementNotFoundError: {'title': '微信测试版', 'top_level_only': True, 'backend': 'uia'}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-9-19 04:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表