问题描述
如何对特定文件执行上下文菜单操作?
我设法打开资源管理器,并使用pywinauto通过python获取文件列表.
How can i do a context menu action on a particular file?
I managed to open the explorer and get the list of files through python using pywinauto.
在该文件上,我需要执行上下文菜单操作,可以通过pywinauto吗?
On that file I need to perform a context menu action, is it possible through 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)
上面的代码将打开资源管理器并导航到该目录.这是对文件执行的上下文菜单操作:
The code above will open the explorer and navigate to the dir. This is the Context menu action required on the file:
我设法找到reg值,并更改了代码以将该操作传递给文件,效果很好!
I managed to find the reg value and changed my code to pass that action to the file, It works perfect!!
pywinauto.Application().start(r'"C:\Program Files (x86)\Qualcomm\QCAT 6.x\Bin\QCAT.exe" -txt "{}"'.format(fileName))
推荐答案
糟糕!没人阅读文档...主要自述文件中提供了示例: MS UI自动化示例.对于您的情况,它看起来应该像这样:
Arrgh! Nobody reads the docs... The example is provided in the main Readme: MS UI Automation Example. For your case it should look like that:
# 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...
有关后端的更多详细信息:入门指南.
More details about backends: Getting Started Guide.
这篇关于如何使用python 3对文件执行上下文菜单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!