如何使用 python 3 对文件执行上下文菜单操作

How to perform a context menu action on a file using python 3

如何对特定文件执行上下文菜单操作?
我设法打开资源管理器并使用 pywinauto python 获取文件列表。

我需要在该文件上执行上下文菜单操作,是否可以通过 pywinauto 执行?

import pywinauto

path = "C:\Users\Vishnu\Desktop\DM-test\"

pywinauto.Application().Start(r'explorer.exe')
explorer = pywinauto.Application().Connect(path='explorer.exe')
NewWindow = explorer.Window_(top_level_only=True, active_only=True,  class_name='CabinetWClass')
NewWindow.AddressBandRoot.ClickInput()
NewWindow.TypeKeys(path+'{ENTER}', with_spaces=True, set_foreground=False)

上面的代码将打开资源管理器并导航到目录。这是文件所需的上下文菜单操作:

我设法找到了 reg 值并更改了我的代码以将该操作传递给文件,它完美无缺!!

pywinauto.Application().start(r'"C:\Program Files (x86)\Qualcomm\QCAT 6.x\Bin\QCAT.exe" -txt "{}"'.format(fileName))

啊!没有人阅读文档......主要自述文件中提供了示例:MS UI Automation Example。对于你的情况,它应该是这样的:

# no need to type the path, explorer.exe has a cmd param for that
pywinauto.Application().start(r'explorer.exe "{}"'.format(path))

# backend is important!!!
app = Application(backend="uia").connect(path="explorer.exe")
NewWindow = explorer.Window_(top_level_only=True, active_only=True,  class_name='CabinetWClass')

file_item = NewWindow.ItemsView.get_item('dmlog20180517-121505slot0.dlf')
file_item.right_click_input()
app.ContextMenu["Convert to QCAT Text"].invoke()

# further actions depend on a process / dialog started...

有关后端的更多详细信息:Getting Started Guide