在这个问题之后,我决定使用以下Python代码来修改Windows快捷方式。
它适用于基于英语的快捷方式,但不适用于基于unicode的快捷方式。
如何修改此(或任何其他)代码片段以支持 unicode?
import re, os, pythoncom
from win32com.shell import shell, shellcon
shortcut_path = os.path.join(path_to_shortcut, shortcut_filename)
shortcut = pythoncom.CoCreateInstance (shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (shortcut_path)
destination1 = shortcut.GetPath(0)[0]
destination2 = os.path.join(destination_path, destination_filename)
shortcut.SetPath(destination2)
persist_file.Save(shortcut_path, 0)
假设以下内容是 unicode:
path_to_shortcut
、shortcut_filename
、destination_path
、destination_filename
也许查看这里可能会有所帮助:Python Unicode HOWTO
我猜您需要确保每个字符串都正确编码为 Unicode,并且任何更改都需要保留该编码。那篇文章应该提供您需要的所有信息。
我知道 2 种编辑 unicode 快捷方式的方法,
winshell
和 pylnk3
。
import winshell
shortcut = winshell.Shortcut("D:\\測試.lnk")
# read
print(shortcut.path)
print(shortcut.working_directory)
# edit
shortcut.path = "D:\\測試.png"
shortcut.working_directory = "D:\\"
shortcut.write()
import pylnk3
lnk = pylnk3.parse("D:\\測試.lnk")
# read
print(lnk.path)
print(lnk.work_dir)
# edit
pylnk3.for_file(
lnk_name = "D:\\測試.lnk",
target_file = "D:\\測試.png",
work_dir = "D:\\",
)