Python メモ

win32com を使ってショートカットを作る

↓参考にしたサイト
[python] デスクトップにショートカットを作る | Reincarnation+

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import win32com.client

# ショートカットを作る
def win32_create_shortcut(target_path, shortcut_path):
    shell = win32com.client.Dispatch("WScript.shell")
    shortcut = shell.CreateShortcut(shortcut_path)
    shortcut.TargetPath = target_path
    shortcut.WindowStyle = 1
    shortcut.Save()

ついでに start コマンドみたいな python 関数も作ってみた

# cmd.exe で実行する start みたいな、ファイルを開くやつ
def win32_start(path):
    shell = win32com.client.Dispatch("WScript.shell")
    shell.Run(path)



でも、win32com は標準でついてないのでインストール必要。

win32com 本家

GitHub - mhammond/pywin32: Python for Windows (pywin32) Extensions

バイナリからインストールが簡単らしい
https://github.com/mhammond/pywin32/releases

VS CodePython 関連

Python の選択

> Python: Select Interpreter // Python の実行ファイルを選ぶ

Linter の選択

> Python: Select Linter // linter を選ぶ

VSCode のターミナルからの win32com インストールしてみた

C:/Users/<your-name>/AppData/Local/Programs/Python/Python37/python.exe -m pip install -U pywin32 --user

(Python3.7が入っている前提)