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 ?
Using the
clipboard
moduleFirst, install the
clipboard
module usingpip3
:Using this cross-platform module (Linux, Mac, Windows) is pretty straightforward:
Tkinter-based solution mentioned in Cameron Laird's answer:
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
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:
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.
you might find this thread useful: How does X11 clipboard handle multiple data formats?