使用Python修改Windows unicode快捷方式

发布于 2024-11-27 16:31:16 字数 944 浏览 1 评论 0原文

这个问题之后,我决定使用以下Python代码来修改Windows快捷方式。
它适用于基于英语的快捷方式,但不适用于基于 unicode 的快捷方式。

如何修改此(或任何其他)代码片段以支持 unicode

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

shortcut_path = os.path.join(path_to_shortcut, shortcut_filename)
shortcut = pythoncom.CoCreateInstance (shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (shortcut_path)
destination1 = shortcut.GetPath(0)[0]
destination2 = os.path.join(destination_path, destination_filename)
shortcut.SetPath(destination2)
persist_file.Save(shortcut_path, 0)

假设以下内容是 unicode:path_to_shortcutshortcut_filenamedestination_pathdestination_filename

Following this question, I've settled on the following Python code to modify Windows shortcuts.
It works for English based shortcuts but it doesn't for unicode based shortcuts.

How could this (or any other) snippet be modified to support unicode?

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

shortcut_path = os.path.join(path_to_shortcut, shortcut_filename)
shortcut = pythoncom.CoCreateInstance (shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (shortcut_path)
destination1 = shortcut.GetPath(0)[0]
destination2 = os.path.join(destination_path, destination_filename)
shortcut.SetPath(destination2)
persist_file.Save(shortcut_path, 0)

Assume the following are unicode: path_to_shortcut, shortcut_filename, destination_path, destination_filename

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-04 16:31:16

也许看这里可能会有所帮助: Python Unicode HOWTO

我猜你需要确保每个字符串都正确编码为 Unicode,并且任何更改都需要保留该编码。该文章应该提供您需要的所有信息。

Perhaps looking here may help: Python Unicode HOWTO

I'm guessing you'd need to be sure that each of those strings was properly encoded as Unicode and any changes need to preserve that encoding. That article should provide all the information you'll need.

优雅的叶子 2024-12-04 16:31:16

我知道编辑 unicode 快捷方式的两种方法,winshellpylnk3
我会推荐 winshell 因为 pylnk3 有一些错误。

winshell

import winshell

shortcut = winshell.Shortcut("D:\\測試.lnk")

# read
print(shortcut.path)
print(shortcut.working_directory)

# edit
shortcut.path = "D:\\測試.png"
shortcut.working_directory = "D:\\"
shortcut.write()

pylnk3

import pylnk3

# read
lnk = pylnk3.parse("D:\\測試.lnk")
print(lnk.path)
print(lnk.work_dir)

# edit
pylnk3.for_file(
    lnk_name = "D:\\測試.lnk",
    target_file = "D:\\測試.png",
    work_dir = "D:\\",
)

I know 2 ways to edit unicode shortcuts, winshell and pylnk3.
I would recommend winshell because pylnk3 has some bug.

winshell

import winshell

shortcut = winshell.Shortcut("D:\\測試.lnk")

# read
print(shortcut.path)
print(shortcut.working_directory)

# edit
shortcut.path = "D:\\測試.png"
shortcut.working_directory = "D:\\"
shortcut.write()

pylnk3

import pylnk3

# read
lnk = pylnk3.parse("D:\\測試.lnk")
print(lnk.path)
print(lnk.work_dir)

# edit
pylnk3.for_file(
    lnk_name = "D:\\測試.lnk",
    target_file = "D:\\測試.png",
    work_dir = "D:\\",
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文