13

I want my Python script to be able to copy and paste to/from the clipboard via x11 (so that it will work on Linux). Can anyone point me to specific resources I can look at, or the concepts I would have to master?

Is this possible to do with the Python X library at http://python-xlib.sourceforge.net ?

Alvin SMith
  • 161
  • 1
  • 4
  • note that, using `xlib`, or other solutions in answers below, will only copy/paste text ; whereas most applications nowadays copy/paste also image or other formats: for those, you may need `gtk` or `qt` libraries. Also the X11 clipboard is pasted using the middle mouse button, but not with ctrl-v , in most applications. – am70 Jan 31 '22 at 10:56

5 Answers5

6

Tkinter-based solution mentioned in Cameron Laird's answer:

import Tkinter
root = Tkinter.Tk()
print(root.selection_get(selection="CLIPBOARD"))

Replace "CLIPBOARD" with "PRIMARY" to get PRIMARY selection instead.

Also see this answer.

python-xlib solution, based on PrintSelection() and python-xlib/examples/get_selection.py

from Xlib import X, display as Xdisplay

def property2str(display, prop):
    if prop.property_type == display.get_atom("STRING"):
        return prop.value.decode('ISO-8859-1')
    elif prop.property_type == display.get_atom("UTF8_STRING"):
        return prop.value.decode('UTF-8')
    else:
        return "".join(str(c) for c in prop.value)

def get_selection(display, window, bufname, typename):
    bufid = display.get_atom(bufname)
    typeid = display.get_atom(typename)
    propid = display.get_atom('XSEL_DATA')
    incrid = display.get_atom('INCR')

    window.change_attributes(event_mask = X.PropertyChangeMask)
    window.convert_selection(bufid, typeid, propid, X.CurrentTime)
    while True:
        ev = display.next_event()
        if ev.type == X.SelectionNotify and ev.selection == bufid:
            break

    if ev.property == X.NONE:
        return None # request failed, e.g. owner can't convert to target format type
    else:
        prop = window.get_property(propid, X.AnyPropertyType, 0, 2**31-1, 1)

        if prop.property_type == incrid:
            result = ""
            while True:
                while True:
                    ev = display.next_event()
                    if ev.type == X.PropertyNotify and ev.atom == propid and ev.state == X.PropertyNewValue:
                        break

                prop = window.get_property(propid, X.AnyPropertyType, 0, 2**31-1, 1)
                if len(prop.value) == 0:
                    break

                result += property2str(display, prop)
            return result
        else:
            return property2str(display, prop)

display = Xdisplay.Display()
window = display.screen().root.create_window(0,0, 1,1, 0, X.CopyFromParent)
print( get_selection(display, window, "CLIPBOARD", "UTF8_STRING") or \
       get_selection(display, window, "CLIPBOARD", "STRING") )
x11user
  • 599
  • 6
  • 9
3

I favor a Tkinter-based solution over one which requires pygtk, simply because of the potential the latter has for installation challenges. Given this, my recommendation to Alvin Smith is to read: Cut & Paste Text Between Tkinter Widgets

You can use this code in a Tkinter event handler (from python-list via Tkinter Clipboard access):

data =  event.widget.selection_get(selection="CLIPBOARD"))
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Cameron Laird
  • 1,067
  • 5
  • 9
1

You can do this with pygtk. A clean solution but might be a bit overkill depending on your application.

Another method that gets some google-hits is to make a system call to xsel.

Mark
  • 106,305
  • 20
  • 172
  • 230
0

you might find this thread useful: How does X11 clipboard handle multiple data formats?

Community
  • 1
  • 1
mykhal
  • 19,175
  • 11
  • 72
  • 80
0

Using the clipboard module

First, install the clipboard module using pip3:

$ sudo pip3 install clipboard

Using this cross-platform module (Linux, Mac, Windows) is pretty straightforward:

import clipboard
clipboard.copy('text')   # Copy to the clipboard.
text = clipboard.paste()   # Copy from the clipboard.
Community
  • 1
  • 1
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102