[英]Modify Windows shortcuts using Python
如何使用Python更改Windows快捷方式?
例如來自:
H:\My Music\some_file.mp3
至:
D:\Users\Myself\My Music\some_file.mp3
這是使用Winshell庫在Python中執行此操作的另一種更合適的方法: 使用Python創建Windows快捷方式 。 在您的情況下,代碼將如下所示:
import os, winshell
from win32com.client import Dispatch
desktop = winshell.desktop()
path = os.path.join(desktop, "some_file.mp3.lnk")
target = r"D:\Users\Myself\My Music\some_file.mp3"
wDir = r"D:\Users\Myself\My Music"
icon = r"D:\Users\Myself\My Music\some_file.mp3"
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()
應刪除或重寫現有快捷方式。 如果你需要它來批量處理快捷方式文件,那么我認為有一些方法可以從現有的快捷方式中讀取路徑,但是沒有設法找到它。
Jonathan的解決方案非常有效。 這是我實現這個的有用功能。 只需傳入快捷方式文件的名稱(例如“Mozilla Firefox.lnk”,不必指定整個文件路徑)和新的快捷方式目標,它就會被修改。
import os, sys
import pythoncom
from win32com.shell import shell, shellcon
def short_target(filename,dest):
shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
shortcut_path = os.path.join (desktop_path, filename)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (shortcut_path)
shortcut.SetPath(dest)
mydocs_path = shell.SHGetFolderPath (0, shellcon.CSIDL_PERSONAL, 0, 0)
shortcut.SetWorkingDirectory (mydocs_path)
persist_file.Save (shortcut_path, 0)
唯一的依賴是pywin32庫。 另請注意,可以在快捷方式目標中指定選項和參數。 要實現,只需調用:
short_target("shortcut test.lnk",'C:\\') #note that the file path must use double backslashes rather than single ones. This is because backslashes are used for special characters in python (\n=enter, etc) so a string must contain two backslashes for it to be registered as one backslash character.
此示例將桌面上名為“快捷方式測試”的快捷方式的目標設置為在硬盤驅動器根目錄(C :)中打開文件管理器的快捷方式。
以下是使用Windows腳本宿主創建快捷方式的方法: http : //msdn.microsoft.com/en-us/library/fywyxt64
嘗試從Python將其寫入文件並動態運行。
之前的答案是完全有效的,但要真正完成它們我添加了批量編輯的代碼,因為我想你可能有很多鏈接要編輯。
如果要一次編輯多個鏈接,請使用此選項:
import os, sys
import glob
import pythoncom
from win32com.shell import shell, shellcon
def shortcut_target (filename):
link = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
persist_file = link.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (filename)
#
# GetPath returns the name and a WIN32_FIND_DATA structure
# which we're ignoring. The parameter indicates whether
# shortname, UNC or the "raw path" are to be
# returned. Bizarrely, the docs indicate that the
# flags can be combined.
#
name, _ = link.GetPath (shell.SLGP_UNCPRIORITY)
target = name
target = target.replace('H:\My Music', 'D:\Users\Myself\My Music')
link.SetPath(target)
persist_file.Save(filename, 0)
return name
def shell_glob (pattern):
for filename in glob.glob (pattern):
if filename.endswith (".lnk"):
print shortcut_target(filename)
desktop = "H:\My Music\"
for filename in shell_glob (os.path.join (desktop, "*")):
print filename
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.