【问题标题】:How to perform a context menu action on a file using python 3如何使用 python 3 对文件执行上下文菜单操作
【发布时间】:2018-11-12 07:35:46
【问题描述】:

如何对特定文件执行上下文菜单操作?
我设法打开资源管理器并使用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))

【问题讨论】:

  • 为什么不直接从注册表中读出所需的操作并直接在文件上执行呢?做一些非常基本的事情似乎还有很长的路要走......
  • 主自述文件中的explorer.exe 示例有什么问题? scroll to the middle of page
  • @zwer 感谢您的建议,我设法找到了 reg 值并更改了我的代码以将该操作传递给文件,它完美无缺! pywinauto.Application().start(r'"C:\Program Files (x86)\Qualcomm\QCAT 6.x\Bin\QCAT.exe" -txt "{}"'.format(i))

标签: python automation contextmenu pywinauto


【解决方案1】:

啊!没有人阅读文档...示例在主自述文件中提供: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。

【讨论】:

  • 嘿 Vasily,我目前正在使用这种类似的方法,但遇到了单击上下文菜单的子菜单部分的问题。例如,app.ContextMenu["Convert to QCAT Text->Sub Menu Item"].invoke()。你知道我可以走的路吗?
  • 我也在这里发布了我的问题 -> link
猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
相关资源
最近更新 更多