Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Getting and setting clipboard data with subprocesses

I recently discovered from this post a way to get and set clipboard data in python via subprocesses, which is exactly what I need for my project.

import subprocess

def getClipboardData():
    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    retcode = p.wait()
    data = p.stdout.read()
    return data

def setClipboardData(data):
    p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
    p.stdin.write(data)
    p.stdin.close()
    retcode = p.wait()

However it only seems to work on the OS X operating system. How can I recreate this functionality across windows, mac and linux?

UPDATE

Using my original code and the windows solution bigbounty provided, I guess I only need a solution for linux now. Perhaps something utilizing xclip or xsel?

like image 239
DejaVu_Loop Avatar asked May 09 '17 02:05

DejaVu_Loop


People also ask

How do you get the Clipboard text in Python?

In Python, you can copy text (string) to the clipboard and paste (get) text from the clipboard with pyperclip. You can also monitor the clipboard to get the text when updated. asweigart/pyperclip: Python module for cross-platform clipboard functions.

How read data from clipboard in Python?

You can use the module called win32clipboard, which is part of pywin32.

How do I paste data from clipboard in Python?

To copy text to the clipboard, pass a string to pyperclip. copy() . To paste the text from the clipboard, call pyperclip. paste() and the text will be returned as a string value.

How do you create a subprocess in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.


2 Answers

For Linux you could use your original code using the xclip utility instead of pbpaste/pbcopy:

import subprocess

def getClipboardData():
    p = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
    retcode = p.wait()
    data = p.stdout.read()
    return data

def setClipboardData(data):
    p = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
    p.stdin.write(data)
    p.stdin.close()
    retcode = p.wait()

xclip's parameters:

  • -selection clipboard: operates over the clipboard selection (X Window have multiple "clipboards"
  • -o: read from desired selection

You should notice that this solution operates over binary data. To storing a string you can use:

setClipboardData('foo'.encode())

And lastly if you are willing to use your program within a shell piping look my question about a issue.

like image 90
mglart Avatar answered Oct 20 '22 23:10

mglart


For windows,

import win32clipboard

# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()

# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print data

Single library across all platforms - http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/

like image 42
bigbounty Avatar answered Oct 20 '22 23:10

bigbounty