python3.6 使用unicode创建win32快捷方式

Per*_*nce 3 python pywin32 win32com python-3.6

我有python3.6创建 Windows 快捷方式的代码:

\n\n
from win32com.client import Dispatch\npath_to_target = r"C:\\Program Files\\\xe3\x83\x94\xe3\x83\x81\xe3\x83\xa3\xe3\x83\xbc\xe3\x83\xa0\\pycharm64.exe"\npath_to_shortcut = r"C:\\Program Files\\pycharm.lnk"\nshell = Dispatch("WScript.Shell")\nshortcut = shell.CreateShortCut(path_to_shortcut)\xe2\x80\xa8            \nshortcut.Targetpath = path_to_target  # exception here\nshortcut.save()\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果path_to_target包含任何非 ASCII 字符,我会得到一个异常:Property \'<unknown>.Targetpath\' can not be set.

\n\n

path_to_target如果只有 ascii 字符,代码可以正常工作并创建正确的快捷方式。

\n\n

如何创建具有 unicode 字符的目标的快捷方式?

\n\n

是否有替代 API 来创建 Windows 快捷方式?

\n

Tar*_*ani 5

这可以通过确保不使用 shell 而是使用直接ShellLink对象来完成

\n\n
import comtypes\nimport comtypes.shelllink\nimport comtypes.client\nimport comtypes.persist\n\nshortcut = comtypes.client.CreateObject(comtypes.shelllink.ShellLink)\nshortcut_w = shortcut.QueryInterface(comtypes.shelllink.IShellLinkW)\nshortcut_file = shortcut.QueryInterface(comtypes.persist.IPersistFile)\n\nshortcut_w.SetPath ("C:\\\\Temp\\\\\xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\x82\xe0\xa4\xa6\xe0\xa5\x80 \xe0\xa4\x9f\xe0\xa4\xbe\xe0\xa4\xaf\xe0\xa4\xaa\xe0\xa4\xbf\xe0\xa4\x82\xe0\xa4\x97.txt")\nshortcut_file.Save("C:\\\\Temp\\\\\xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\x82\xe0\xa4\xa6\xe0\xa5\x80.lnk", True)\n
Run Code Online (Sandbox Code Playgroud)\n\n

已创建

\n\n

更新1

\n\n

感谢@Axois 评论,我已经验证如果您设置 unicode 支持,您的原始代码可以工作

\n\n

统一码支持

\n\n

PS:评论在问题的评论为我指明了正确的方向

\n