I've a script that scrapes data from a third party program. Currently I'm using emulated keyboard strokes to select and copy data:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('^a')
shell.SendKeys('^c')
The script works fine, but for some reason it makes my system and the third party app very laggy. Now I'm looking more efficient ways to select all and copy in Windows.
Yes, TeraCopy is a faster data transfer tool than Windows 10. In HowToGeek, there was actually a battle of file copiers where a large number of files were tried and tested using the regular Windows Copier and TeraCopy.
The other way i can think of is to use the Inspect.
For instances, I've got this question opened in a Chrome window and that's the result in Inspect
From the previous and following, we can see due to the blue color that i'm looking at the data related to that browser window.
Once selected, press CTRL + Shift + 4 at the same time, or go to Edit > Copy All. This gives all the information that appears on the right side of the image above, namely
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 ]
and we can extract what we want from it.
Let's say our goal is to get all text in your question.
We visualize each window or program as the superior node of a tree.
We would have to first go to the First Child by pressing Ctrl + Shift + F7 at the same time (we could have also pressed Ctrl + Shift + F9 at the same time to go to the Last Child but in this case was faster the other way - to note that the image doesn't show all the windows / programs i have opened). This would go one level down the tree, which reads Inspect (HWND: 0x001306A2) UIAccess.
Because that's not the window / program we want, we have to do something about it. To reach the one we want we have to navigate to the Next Sibling, then to the Next Sibling and to the Next Sibling. So we would have to press Ctrl + Shift + F8 at the same time for three times. If we ran it four times instead of three, we could use press Ctrl + Shift + F5 at the same time (Previous Sibling).
In this specific case, we found the text of the question divided into three parts
From looking at Name in the right part of the image (the area we can copy pressing CTRL + Shift + 4 at the same time) we see
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:"
The other two siblings of text have the rest of the information, so we would have to visit them one by one and copy their content (to go back to the parent we press CTRL + Shift + F6 at the same time).
Note: If i knew what third party program you were using and the specific content you want I could give a more detailed explanation, but with the previous example it's already possible to see how to do it.
One trick is to combine the sendkey commands to improve the speed, although I feel that it is not the main concern for your program.
shell.SendKeys('^a^c')
If you are running this script many times in a loop, it could also cause your system to become slow, so adding a delay to it would help.
You could also make an AutoIt script files if you're open to using other languages and are only looking for automating a simple select-all + copy (although it is not in Python). It would just run as a background task that takes very little computing power and can be bound to run on any key, eg. 'z'.
HotKeySet ( "z" [, "select_copy"] )
While 1
Sleep(100)
WEnd
Func select_copy()
send("^a")
send("^c")
EndFunc
Hope these help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With