python实现应用程序在右键菜单中添加打开方式功能

yipeiwu_com5年前Python基础

最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写

1.需求拆解:

如何实现手动添加右键菜单的打开方式:

Step1:打开注册表编辑器,Win+R->输入 “regedit”

Step2:在HKEY_CLASSES_ROOT/*/shell (或者HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell ,两个目录是一样的) 添加一个key:YNote,然后在该项中新建项command,然后再编辑字符串,添加应用程序的路径,最后再路径和名称的后面加上空格和“%1”,然后在右键就可以找到YNote的打开方式

2.代码实现

Method1:通过_winreg模块实现:

import _winreg
from _winreg import KEY_ALL_ACCESS
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Classes\*\shell") as key:
print key
newKey = _winreg.CreateKeyEx(key,"YNote",0,KEY_ALL_ACCESS)
sub_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")
newsubKey = _winreg.CreateKey(sub_key,"command")
_winreg.SetValue(newsubKey,"(Default)",1,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")

Method2:通过win32api和win32con模块实现

import win32api
import win32con
key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell")
newKey = win32api.RegCreateKey(key,"YNote")
sub_key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")
newsubKey = win32api.RegCreateKey(sub_key,"command")
win32api.RegSetValue(newsubKey,"(Default)", win32con.REG_SZ,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")

以上所述是小编给大家介绍的python实现应用程序在右键菜单中添加打开方式功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

使用Python实现图像标记点的坐标输出功能

使用Python实现图像标记点的坐标输出功能

Sometimes we have need to interact  with an application,for example by marking points in...

Python图像处理PIL各模块详细介绍(推荐)

Python图像处理PIL各模块详细介绍(推荐)

 Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内。如open、save、conver、show…等功能...

Python实现的中国剩余定理算法示例

本文实例讲述了Python实现的中国剩余定理算法。分享给大家供大家参考,具体如下: 中国剩余定理(Chinese Remainder Theorem-CRT):又称孙子定理,是数论中的一...

django fernet fields字段加密实践详解

一、fernet介绍 Fernet 用于django模型字段对称加密,使用 crytography 库。 官网帮助文档 1、先决条件 django-fernet-fields 支持D...

使用Python的Twisted框架编写简单的网络客户端

Protocol   和服务器一样,也是通过该类来实现。先看一个简短的例程: from twisted.internet.protocol import Protocol...