在Windows中全选和复制的最有效方法

jjj*_*ayn 7 python windows win32gui win32com

我有一个脚本,可从第三方程序中抓取数据。目前,我正在使用模拟的键盘笔触来选择和复制数据:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('^a')
shell.SendKeys('^c')
Run Code Online (Sandbox Code Playgroud)

该脚本可以正常运行,但是由于某种原因,它使我的系统和第三方应用程序非常滞后。现在,我正在寻找更有效的方法来全选并在Windows中复制。

Tia*_*ica 4

长话短说

我能想到的另一种方法是使用Inspect

例如,我在 Chrome 窗口中打开了这个问题,这就是 Inspect 中的结果

检查堆栈溢出质量检查

从前面和后面的内容中,我们可以看到由于蓝色,我正在查看与该浏览器窗口相关的数据。

检查

选择后,同时按 CTRL + Shift + 4,或转到“编辑”>“复制全部”。这给出了上图右侧出现的所有信息,即

How found:  Selected from tree...
Name:   "python - Most efficient way to select all and copy in Windows - Stack Overflow - Google Chrome"
ControlType:    UIA_PaneControlTypeId (0xC371)
LocalizedControlType:   "pane"
BoundingRectangle:  {l:1358 t:-8 r:3294 b:1048}
IsEnabled:  true
IsOffscreen:    false
IsKeyboardFocusable:    false
HasKeyboardFocus:   false
AccessKey:  ""
ProcessId:  8396
RuntimeId:  [2A.3508A4]
FrameworkId:    "Win32"
ClassName:  "Chrome_WidgetWin_1"
NativeWindowHandle: 0x3508A4
IsControlElement:   true
ProviderDescription:    "[pid:12412,providerId:0x3508A4 Main:Nested [pid:8396,providerId:0x3508A4 Annotation(parent link):Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main:Microsoft: MSAA Proxy (IAccessible2) (unmanaged:uiautomationcore.dll)]; Nonclient:Microsoft: Non-Client Proxy (unmanaged:uiautomationcore.dll); Hwnd(parent link):Microsoft: HWND Proxy (unmanaged:uiautomationcore.dll)]"
IsPassword: false
IsRequiredForForm:  false
IsDataValidForForm: true
HelpText:   ""
Culture:    0
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    ""
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: ""
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "python - Most efficient way to select all and copy in Windows - Stack Overflow - Google Chrome"
LegacyIAccessible.Role: pane (0x10)
LegacyIAccessible.State:    normal (0x0)
LegacyIAccessible.Value:    ""
Transform.CanMove:  false
Transform.CanResize:    false
Transform.CanRotate:    false
Window.CanMaximize: true
Window.CanMinimize: true
Window.IsModal: false
Window.IsTopmost:   false
Window.WindowInteractionState:  ReadyForUserInteraction (2)
Window.WindowVisualState:   Maximized (1)
IsAnnotationPatternAvailable:   false
IsDragPatternAvailable: false
IsDockPatternAvailable: false
IsDropTargetPatternAvailable:   false
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   false
IsItemContainerPatternAvailable:    false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsObjectModelPatternAvailable:  false
IsRangeValuePatternAvailable:   false
IsScrollItemPatternAvailable:   true
IsScrollPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsSpreadsheetItemPatternAvailable:  false
IsSpreadsheetPatternAvailable:  false
IsStylesPatternAvailable:   false
IsSynchronizedInputPatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTablePatternAvailable:    false
IsTextChildPatternAvailable:    false
IsTextEditPatternAvailable: false
IsTextPatternAvailable: false
IsTextPattern2Available:    false
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    true
IsTransform2PatternAvailable:   false
IsValuePatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsWindowPatternAvailable:   true
IsCustomNavigationPatternAvailable: false
IsSelectionPattern2Available:   false
FirstChild: "python - Most efficient way to select all and copy in Windows - Stack Overflow" document
LastChild:  "Google Chrome" pane
Next:   "Accessibility tools - Inspect - Windows applications | Microsoft Docs - Google Chrome" pane
Previous:   "" pane
Other Props:    Object has no additional properties
Children:   "python - Most efficient way to select all and copy in Windows - Stack Overflow" document
    "" pane
    (null) title bar
    "Google Chrome" pane
Ancestors:  "Desktop 1" pane
    [ No Parent ]
Run Code Online (Sandbox Code Playgroud)

我们可以从中提取我们想要的东西。

实际例子

假设我们的目标是获取您问题中的所有文本。

检查窗户

我们将每个窗口或程序可视化为树的上级节点。

我们必须首先同时按 Ctrl + Shift + F7 转到第一个子项(我们也可以同时按 Ctrl + Shift + F9 转到最后一个子项,但在这种情况下,另一个速度更快)方式 - 请注意,图像并未显示我打开的所有窗口/程序)。这将在树中向下一层,显示Inspect (HWND: 0x001306A2) UIAccess

因为那不是我们想要的窗口/程序,所以我们必须对此做点什么。为了到达我们想要的,我们必须导航到下一个兄弟姐妹,然后导航到下一个兄弟姐妹和下一个兄弟姐妹。所以我们必须同时按 Ctrl + Shift + F8 三次。如果我们运行四次而不是三次,我们可以同时按 Ctrl + Shift + F5(前一个兄弟)。

在这个具体案例中,我们发现问题的文本分为三个部分

检查树中的导航

从图像右侧部分的名称(我们可以同时按 CTRL + Shift + 4 复制的区域)我们看到

Name:   "I've a script that scrapes data from a third party program. Currently I'm using emulated keyboard strokes to select and copy data:"
Run Code Online (Sandbox Code Playgroud)

文本的另外两个兄弟姐妹拥有其余信息,因此我们必须一一访问它们并复制其内容(要返回父级,我们同时按 CTRL + Shift + F6)。

注意:如果我知道您使用的第三方程序以及您想要的具体内容,我可以给出更详细的解释,但通过前面的示例,已经可以了解如何做到这一点。