简体   繁体   English

“打开方式...” Windows 上的文件,带有 python 应用程序

[英]"Open with..." a file on Windows, with a python application

I am trying to figure out how to make a python program open a file when a user right clicks on the file and selects "Open With".我试图弄清楚当用户右键单击文件并选择“打开方式”时如何使 python 程序打开文件。 For example, I want a user to be able right click on a text file and to select my program so that my program can process the text file.例如,我希望用户能够右键单击文本文件和 select 我的程序,以便我的程序可以处理文本文件。 Is the name of the text file passed into my program someway?文本文件的名称是否以某种方式传递到我的程序中? Thanks.谢谢。

My approach is to use a redirect .bat file containing python someprogram.py %1 .我的方法是使用包含python someprogram.py %1的重定向 .bat 文件。 The %1 passes the file path into the python script which can be accessed with %1将文件路径传递到可以使用以下命令访问的 python 脚本中
from sys import argv argv[1]

The problem with this approach is that your .py file is not an executable;这种方法的问题在于您的 .py 文件不是可执行文件; Windows will pass the text file as parameter to the .py file, but the .py file itself will not do anything, since it's not an executable file. Windows会将文本文件作为参数传递给 .py 文件,但 .py 文件本身不会执行任何操作,因为它不是可执行文件。

What you can do is compile your script with py2exe to get an actual executable, which you can actually specify in the "Open With..." screen (you could even register it as the default for any *.foo file).您可以做的是使用py2exe编译您的脚本以获得实际的可执行文件,您实际上可以在“打开方式...”屏幕中指定它(您甚至可以将其注册为任何 *.foo 文件的默认值)。 The path to the .foo file being passed should be sys.argv[1] in your script.传递的 .foo 文件的路径应该是脚本中的sys.argv[1]

First you will need to register your script to run with Python under a ProgId in the registry.首先,您需要在注册表中的 ProgId 下注册您的脚本以使用 Python 运行。 At a minimum, you will need the open verb defined:至少,您需要定义开放动词:

HKEY_CURRENT_USER\Software\Classes\MyApp.ext\
  (Default) = "Friendly Name"
  DefaultIcon\
    (Default) = "path to .ico file"
  shell\
    open\
      command\
        (Default) = 'path\python.exe "path\to\your\script.py" "%L"'

You can substitute HKEY_LOCAL_MACHINE if you are installing machine-wide.* There are also versioning conventions that you can probably ignore.如果您在机器范围内安装,您可以替换HKEY_LOCAL_MACHINE 。* 还有一些您可能可以忽略的版本约定。 The MSDN section on File Types has more detailed information.关于文件类型的 MSDN 部分有更详细的信息。

The second step is to add your ProgId to the OpenWithProdIds key of the extension you want to appear in the list for:第二步是将您的 ProgId 添加到您想要出现在列表中的扩展的OpenWithProdIds键:

HKEY_CURRENT_USER\Software\Classes\.ext\OpenWithProgIds
  MyApp.ext = None

The value of the key does not matter, as long as the name matches your ProgId exactly.键的值无关紧要,只要名称与您的 ProgId 完全匹配即可。


*Note that HKEY_CLASSES_ROOT is actually a fake key that 'contains' a union of both HKLM\\Software\\Classes and HKCU\\Software\\Classes ; *请注意, HKEY_CLASSES_ROOT实际上是一个假密钥,它“包含”了HKLM\\Software\\ClassesHKCU\\Software\\Classes if you're writing to the registry, you should choose one of the actual keys.如果您正在写入注册表,您应该选择实际的键之一。 You don't need to elevate to install into HKEY_CURRENT_USER .您不需要提升安装到HKEY_CURRENT_USER

@roy cai's answer is right, it just needs some modifications. @roy cai 的回答是对的,它只需要一些修改。

According to here , the someprogram.bat should contain根据这里, someprogram.bat 应该包含

start someprogram.py(w) %*

Just give your cmdline args to the bat file.只需将您的 cmdline 参数提供给 bat 文件即可。 The %* takes up all the cmdline args, according to here from here . %*占用所有 cmdline 参数,根据here from here

simple example:简单的例子:

import sys
try:
    #if "open with" has been used
    print(sys.argv[1])
except:
    #do nothing
    pass

usage example:用法示例:

import sys
from tkinter import filedialog

filetypes = (('Text files', '*.txt'),('All files', '*.*'))

#if filename is not specified, ask for a file
def openfile(filename = filedialog.askopenfilename(title='Open A File',filetypes=filetypes)):
    #print contents of file
    with open(filename,'r', encoding="utf-8") as file:
        read = file.read()
    print(read)



try:
    #if "open with" has been used
    openfile(filename = sys.argv[1])
except:
    #ask for a file
    openfile()

then compile it to exe with nuitka (or whatever tool you use),然后使用nuitka(或您使用的任何工具)将其编译为exe,
and try it.试试看。
or (for testing, without having to compile it every time you make a change):或(用于测试,每次更改时都无需编译):
make a.py file制作一个.py文件

from subprocess import call
from sys import argv
call(f'py print.py {argv[1]}')

then compile that to exe,然后编译exe,
so you dont have to compile the actual program every time.所以你不必每次都编译实际的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM