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

余生颓废 提交于 2020-02-25 09:57:08

问题


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.

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:

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))


回答1:


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.



来源:https://stackoverflow.com/questions/50655635/how-to-perform-a-context-menu-action-on-a-file-using-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!