Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3.6 create win32 shortcut with unicode

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?

like image 375
Periodic Maintenance Avatar asked Dec 06 '25 14:12

Periodic Maintenance


1 Answers

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)

Created

Update 1

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

Unicode support

PS: Comments in this question pointed me in the right direction

like image 179
Tarun Lalwani Avatar answered Dec 09 '25 02:12

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!