我有创建 Windows 快捷方式的 python3.6
代码:
from win32com.client import Dispatch
path_to_target = r"C:\Program Files\ピチャーム\pycharm64.exe"
path_to_shortcut = r"C:\Program Files\pycharm.lnk"
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path_to_shortcut)
shortcut.Targetpath = path_to_target # exception here
shortcut.save()
如果 path_to_target
包含任何非 ascii 字符,我会得到一个异常:Property '<unknown>.Targetpath' cannot be set.
如果 path_to_target
只是 ascii 字符,代码工作正常并创建正确的快捷方式。
如何创建指向具有 unicode 字符的目标的快捷方式?
是否有替代 API 来创建 Windows 快捷方式?
最佳答案
这可以通过确保不使用 shell 而直接使用 ShellLink
对象来完成
import comtypes
import comtypes.shelllink
import comtypes.client
import comtypes.persist
shortcut = comtypes.client.CreateObject(comtypes.shelllink.ShellLink)
shortcut_w = shortcut.QueryInterface(comtypes.shelllink.IShellLinkW)
shortcut_file = shortcut.QueryInterface(comtypes.persist.IPersistFile)
shortcut_w.SetPath ("C:\\Temp\\हिंदी टायपिंग.txt")
shortcut_file.Save("C:\\Temp\\हिंदी.lnk", True)
更新 1
感谢@Axois 的评论,如果您设置了 unicode 支持,我已经验证您的原始代码可以正常工作
PS:评论在this问题为我指明了正确的方向
关于python3.6用unicode创建win32快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57179274/