Create shortcut files in Windows 10 using Python 3.7.1












-1














I found this piece of code, but it doesn't run anymore with Windows 10 and Python 3.7.1:



import win32com.client
import pythoncom
import os
# pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
desktop = r'C:UsersXXXXXDesktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:UsersXXXXXDesktopmuellpicture.gif'
icon = r'C:UsersXXXXXDesktopmuellicons8-link-512.ico' # not needed, but nice

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()


Is there a similar (or easier) way to create a windows shortcut?










share|improve this question






















  • How come the code does not run? It's just Python. It should still run, if you have Python installed.
    – usr2564301
    Nov 25 '18 at 16:56










  • Yes, but it doesn't.
    – merlinuwe
    Nov 26 '18 at 20:18










  • What does "doesn't run" mean, then? Nothing happens at all?
    – usr2564301
    Nov 26 '18 at 21:36










  • I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
    – merlinuwe
    Nov 28 '18 at 7:49


















-1














I found this piece of code, but it doesn't run anymore with Windows 10 and Python 3.7.1:



import win32com.client
import pythoncom
import os
# pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
desktop = r'C:UsersXXXXXDesktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:UsersXXXXXDesktopmuellpicture.gif'
icon = r'C:UsersXXXXXDesktopmuellicons8-link-512.ico' # not needed, but nice

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()


Is there a similar (or easier) way to create a windows shortcut?










share|improve this question






















  • How come the code does not run? It's just Python. It should still run, if you have Python installed.
    – usr2564301
    Nov 25 '18 at 16:56










  • Yes, but it doesn't.
    – merlinuwe
    Nov 26 '18 at 20:18










  • What does "doesn't run" mean, then? Nothing happens at all?
    – usr2564301
    Nov 26 '18 at 21:36










  • I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
    – merlinuwe
    Nov 28 '18 at 7:49
















-1












-1








-1







I found this piece of code, but it doesn't run anymore with Windows 10 and Python 3.7.1:



import win32com.client
import pythoncom
import os
# pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
desktop = r'C:UsersXXXXXDesktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:UsersXXXXXDesktopmuellpicture.gif'
icon = r'C:UsersXXXXXDesktopmuellicons8-link-512.ico' # not needed, but nice

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()


Is there a similar (or easier) way to create a windows shortcut?










share|improve this question













I found this piece of code, but it doesn't run anymore with Windows 10 and Python 3.7.1:



import win32com.client
import pythoncom
import os
# pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
desktop = r'C:UsersXXXXXDesktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:UsersXXXXXDesktopmuellpicture.gif'
icon = r'C:UsersXXXXXDesktopmuellicons8-link-512.ico' # not needed, but nice

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()


Is there a similar (or easier) way to create a windows shortcut?







python-3.x windows shortcut






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:24









merlinuwe

5914




5914












  • How come the code does not run? It's just Python. It should still run, if you have Python installed.
    – usr2564301
    Nov 25 '18 at 16:56










  • Yes, but it doesn't.
    – merlinuwe
    Nov 26 '18 at 20:18










  • What does "doesn't run" mean, then? Nothing happens at all?
    – usr2564301
    Nov 26 '18 at 21:36










  • I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
    – merlinuwe
    Nov 28 '18 at 7:49




















  • How come the code does not run? It's just Python. It should still run, if you have Python installed.
    – usr2564301
    Nov 25 '18 at 16:56










  • Yes, but it doesn't.
    – merlinuwe
    Nov 26 '18 at 20:18










  • What does "doesn't run" mean, then? Nothing happens at all?
    – usr2564301
    Nov 26 '18 at 21:36










  • I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
    – merlinuwe
    Nov 28 '18 at 7:49


















How come the code does not run? It's just Python. It should still run, if you have Python installed.
– usr2564301
Nov 25 '18 at 16:56




How come the code does not run? It's just Python. It should still run, if you have Python installed.
– usr2564301
Nov 25 '18 at 16:56












Yes, but it doesn't.
– merlinuwe
Nov 26 '18 at 20:18




Yes, but it doesn't.
– merlinuwe
Nov 26 '18 at 20:18












What does "doesn't run" mean, then? Nothing happens at all?
– usr2564301
Nov 26 '18 at 21:36




What does "doesn't run" mean, then? Nothing happens at all?
– usr2564301
Nov 26 '18 at 21:36












I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
– merlinuwe
Nov 28 '18 at 7:49






I found the problem by myself: the target was wrong. Here is the correct line: target = r'C:Program Files (x86)Microsoft OfficeOffice16EXCEL.EXE'
– merlinuwe
Nov 28 '18 at 7:49














1 Answer
1






active

oldest

votes


















1














This worked for me: (Win 10, python 2)



http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html



import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)





share|improve this answer





















  • Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
    – merlinuwe
    Nov 26 '18 at 20:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268792%2fcreate-shortcut-files-in-windows-10-using-python-3-7-1%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This worked for me: (Win 10, python 2)



http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html



import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)





share|improve this answer





















  • Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
    – merlinuwe
    Nov 26 '18 at 20:25
















1














This worked for me: (Win 10, python 2)



http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html



import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)





share|improve this answer





















  • Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
    – merlinuwe
    Nov 26 '18 at 20:25














1












1








1






This worked for me: (Win 10, python 2)



http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html



import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)





share|improve this answer












This worked for me: (Win 10, python 2)



http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html



import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 16:39









Petr Lavrov

164




164












  • Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
    – merlinuwe
    Nov 26 '18 at 20:25


















  • Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
    – merlinuwe
    Nov 26 '18 at 20:25
















Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
– merlinuwe
Nov 26 '18 at 20:25




Thank you very much. Could you please explain, how I can built a shortcut to for example excel.exe or C:UsersXXXXXDesktopmuellscript.py with this code?
– merlinuwe
Nov 26 '18 at 20:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268792%2fcreate-shortcut-files-in-windows-10-using-python-3-7-1%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini