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()
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
):
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()
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())
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
)
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
))