16

I'm trying to automate some actions in a browser or a word processor with pyautogui module for Python 3 (Windows 10).

There is a highlighted text in a browser.

text

the following script should print the highlighted text

import pyautogui as pya

# double clicks on a position of the cursor
pya.doubleClick(pya.position())

list = []
# a function copy_clipboard() should be called here
var = copy_clipboard()
list.append(var) 
print(list)

The output should be:

[text]

So how should the function copy_clipboard() look like? Thank you for your help.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
Stefan Smirnov
  • 695
  • 3
  • 6
  • 18
  • this sounds like something requests and beautifulsoup would be great at. Or at least selenium if you want to automate a browser – SuperStew Jul 24 '18 at 18:27
  • Thank you for your reply. I want that script to be working with text editors as well. – Stefan Smirnov Jul 24 '18 at 18:31
  • Is there something special it needs to do with the text editor? – SuperStew Jul 24 '18 at 18:35
  • Just copy a highlighted text (from a text editor/processor or a browser) and assign it to a variable in a script. If I did that manually it should've looked like Ctrl+C, Ctrl+V. – Stefan Smirnov Jul 24 '18 at 18:39
  • Look into using either the `Tkinter` or `ctypes` module. Here's the `ctypes` [solution](https://stackoverflow.com/a/25678113/6221024) I've used in the past. – Dillon Davis Jul 24 '18 at 18:47

5 Answers5

25

The keyboard combo Ctrl+C handles copying what is highlighted in most apps, and should work fine for you. This part is easy with pyautogui. For getting the clipboard contents programmatically, as others have mentioned, you could implement it using ctypes, pywin32, or other libraries. Here I've chosen pyperclip:

import pyautogui as pya
import pyperclip  # handy cross-platform clipboard text handler
import time

def copy_clipboard():
    pya.hotkey('ctrl', 'c')
    time.sleep(.01)  # ctrl-c is usually very fast but your program may execute faster
    return pyperclip.paste()

# double clicks on a position of the cursor
pya.doubleClick(pya.position())

list = []
var = copy_clipboard()
list.append(var) 
print(list)
Zephyr
  • 11,891
  • 53
  • 45
  • 80
soundstripe
  • 1,454
  • 11
  • 19
  • 1
    That solution did work. And thank you for explaining why there should be a pause in the script - that was not obvious to me. – Stefan Smirnov Jul 24 '18 at 20:19
5

Example using tkinter:

from tkinter import Tk
import pyautogui as pya

def copy_clipboard():
    root = Tk()     # Initialize tkinter
    root.withdraw() # hide the tkinter window
    pya.hotkey("ctrl", "c") # copy the text (simulating key strokes)
    clipboard = root.clipboard_get() # get the text from the clipboard
    return clipboard

copy_text = copy_clipboard()
print(copy_text)

Tk().clipboard_get() returns the current text in the clipboard.

Muzol
  • 301
  • 1
  • 11
4

What soundstripe posted is valid, but doesn't take into account copying null values when there was a previous value copied. I've included an additional line that clears the clipboard so null-valued copies remain null-valued:

import pyautogui as pya
import pyperclip  # handy cross-platform clipboard text handler
import time

def copy_clipboard():
    pyperclip.copy("") # <- This prevents last copy replacing current copy of null.
    pya.hotkey('ctrl', 'c')
    time.sleep(.01)  # ctrl-c is usually very fast but your program may execute faster
    return pyperclip.paste()

# double clicks on a position of the cursor
pya.doubleClick(pya.position())

list = []
var = copy_clipboard()
list.append(var) 
print(list)
phfb
  • 121
  • 2
  • 12
0

Another option to get highlighted/selected text:

import subprocess
import shlex
selected_text = subprocess.check_output((shlex.split('xclip -out -selection')))
Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
-1

You could import pyperclip and use pyperclip.copy('my text I want copied') and then use pyperclip.paste() to paste the text wherever you want it to go. You can find a reference here.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • But we assume that the text is in another program maybe a browser or something like that. So how could you pass it as a parameter of `pyperclip.copy`? – Muzol Jul 24 '18 at 19:09
  • @Muzol My apologies; I was unclear on what you wanted. I'm not sure there's a good way to do that. You might be better off using Ctrl + C – Woody1193 Jul 24 '18 at 19:16