吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1430|回复: 5
收起左侧

[学习记录] python windows创建快捷方式

[复制链接]
fengyuwuqing 发表于 2025-8-15 09:59
本帖最后由 fengyuwuqing 于 2025-8-19 10:01 编辑

try:
    import win32com.client
except ImportError:
    print("错误:缺少pywin32库,请先执行以下命令安装:")
    print("pip install pywin32")
    sys.exit(1)

def create_desktop_shortcut(target_path, shortcut_name):
    """在桌面创建指定文件的快捷方式
    target_path:目标文件
    shortcut_name:快捷方式名称
    """
    try:
        # 检查目标文件是否存在
        if not os.path.exists(target_path):
            print(f"错误:目标文件不存在 - {target_path}")
            return False

        # 获取桌面路径
        shell = win32com.client.Dispatch("WScript.Shell")
        desktop_path = shell.SpecialFolders("Desktop")

        # 构建快捷方式路径
        shortcut_path = os.path.join(desktop_path, f"{shortcut_name}.lnk")

        # 创建快捷方式对象
        shortcut = shell.CreateShortCut(shortcut_path)
        shortcut.TargetPath = target_path
        shortcut.WorkingDirectory = os.path.dirname(target_path)  # 设置工作目录
        shortcut.IconLocation = target_path  # 使用程序自身图标
        shortcut.Description = f"{shortcut_name} 快捷方式"  # 描述信息
        shortcut.save()

        print(f"桌面快捷方式已创建:{shortcut_name}.lnk")
        return True
    except Exception as e:
        print(f"创建快捷方式失败:{str(e)}")
        return False

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
jnct + 1 谢谢@Thanks!
shine123 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

linyuuki 发表于 2025-8-15 10:44
用vbs似乎更简单些,不用py环境
xiaomingtt 发表于 2025-8-15 13:38
kyknknt 发表于 2025-8-15 22:03
linyuuki 发表于 2025-8-17 14:42
kyknknt 发表于 2025-8-15 22:03
那个win32com不用import什么包吗

要导入包的,他没写吧
 楼主| fengyuwuqing 发表于 2025-8-19 10:00
kyknknt 发表于 2025-8-15 22:03
那个win32com不用import什么包吗

[Python] 纯文本查看 复制代码
import win32com.client
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-9-15 23:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表