写了 ...
[/quote]
[mw_shl_code=python,true]
'''
Description: Create shortcuts from Poner data json file to desktop
Author: Daxi
Data:
LastEdit:
LastEditTime: 2021-08-22 19:10:13
FileName:
'''
import os
import win32com.client as client
import xml.etree.ElementTree as ET
import pandas as pd
import json
import re
shell = client.Dispatch("WScript.Shell")
def GetShortCut(shortcut):
return shell.CreateShortCut(shortcut).Targetpath
def createShortCut(filename, lnkname):
"""filename should be abspath, or there will be some strange errors"""
shortcut = shell.CreateShortCut(lnkname)
shortcut.TargetPath = filename
shortcut.save()
def CreateShortCut(filename, lnkname):
print(os.path.abspath(filename))
createShortCut(os.path.abspath(filename), lnkname)
# test
# CreateShortCut('test', 'linkname.lnk')
def main():
#a copy of source file
dataPath = r"C:\Users\aja\Desktop\Data.json"
# prefix, the folder path of the tools
prefixPath = r'C:\deSec\52toolsFolder'
# where u want to create the shortcuts(path to save the results)
wksps = r'd:\wksps\py\res'
if not os.path.exists(wksps):
os.mkdir(wksps)
os.chdir(wksps)
data = open(dataPath, "r",encoding='UTF-8')
js = json.load(data)
# print(js)
for folder in js:
idxFolder = wksps + '\\' + folder
if not (os.path.exists(idxFolder)):
os.mkdir(idxFolder)
os.chdir(idxFolder)
for detail in js[folder]:
name = detail['Name']
target = detail['TargetPath'].replace(r'%rp%',prefixPath)
icon = detail['IconLocation']
re_res = re.findall('---------(.+?)---------', name)
if len(re_res) != 0:
print(re_res[0])
tmp_dir = idxFolder + '\\' + re_res[0].replace('/','-')
os.mkdir(tmp_dir)
os.chdir(tmp_dir)
else:
CreateShortCut(target, name + '.lnk')
print(target)
data.close()
if __name__ == '__main__':
main()