I have this python3.6 code that creates Windows shortcuts:
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()
If path_to_target contains any non ascii characters I get an exception: Property '<unknown>.Targetpath' can not be set.
The code works OK and creates the proper shortcut if path_to_target is only ascii characters.
How can I create shortcuts to targets that have unicode characters?
Is there alternative API to create Windows shortcuts?
This can be done by making sure not to use shell but the direct ShellLink object
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)

Update 1
Thanks to @Axois comments, I have verified that your original code works if you set the unicode support

PS: Comments in this question pointed me in the right direction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With