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

标签 python python-3.6 pywin32 win32com

我有创建 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)

Created

更新 1

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

Unicode support

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

关于python3.6用unicode创建win32快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57179274/

相关文章:

python-3.x - 如何使用 pip 获取 python 3.6 的 webbrowser 模块?

python - pygame 中的 pywin32 函数导致程序挂起/"python.exe is not responding"

python - 如何将一系列任务路由到 celery 中的特定队列?

python - 将列表中的一个字符串变成列表中的两个字符串

python - 安装聊天机器人但得到 "ERROR: Could not build wheels for spacy which use PEP 517 and cannot be installed directly"

python-3.6 - 如何使用 apscheduler 在特定时间之间每分钟触发一次作业?

django - 如何在 Django 2.1 中包含 DRF 3.8 路由器 url 模式

python - 如何查看一段时间内收到的所有邮件?

Python Ctypes 键盘事件

python - "TypeError: Using a ` tf.Tensor ` as a Python ` bool ` is not allowed."在数据集上调用map函数时