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") )
Answer from x11user on Stack Overflow
🌐
PyPI
pypi.org › project › clipboard
clipboard · PyPI
A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux.
      » pip install clipboard
    
Published   May 22, 2014
Version   0.0.4
Top answer
1 of 4
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") )
2 of 4
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"))
Discussions

Python - Getting and setting clipboard data with subprocesses - Stack Overflow
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 = More on stackoverflow.com
🌐 stackoverflow.com
linux - Is there a way to directly send a python output to clipboard? - Stack Overflow
For example, if a python script will spit out a string giving the path of a newly written file that I'm going to edit immediately after running the script, it would be very nice to have it directly sent to the system clipboard rather than STDOUT. ... @NullUserExceptionఠ_ఠ I assume it would be, but I'm working entirely inside Linux... More on stackoverflow.com
🌐 stackoverflow.com
How to copy to the clipboard using python?
if you need clipboard support, this might help, without pyperclip Not sure how that works with Qt though. I would assume there are similar implementations. More on reddit.com
🌐 r/linux4noobs
8
3
December 15, 2015
copykitten: the missing clipboard library for Python
“Now I hate windows” 😂🤣 same. I grew up using only Windows machines, never knowing how green the grass can be with other options. More on reddit.com
🌐 r/Python
35
120
February 21, 2024
🌐
Python.org
discuss.python.org › python help
Copy to system clipboard - Python Help - Discussions on Python.org
June 2, 2022 - I’m working on a an app (my 2nd GUI app) that generates a hash value (AKA: checksum) based on a selected file. Right now, the hash value is displayed in a Text Widget, which is has the ‘state’ set to ‘disabled’, so that …
🌐
GitHub
gist.github.com › ShannonScott › 50ffcd12e134b83a8680835a838dcfc0
[Python Copy Text to Clipboard] Copy Text to the Linux / X Clipboard. #tags: linux, python · GitHub
from subprocess import Popen, PIPE def copy_clipboard(msg): ''' Copy `msg` to the clipboard ''' with Popen(['xclip','-selection', 'clipboard'], stdin=PIPE) as pipe: pipe.communicate(input=msg.encode('utf-8')) # Copy some text to the clipboard ...
🌐
AskPython
askpython.com › home › how do i read text from the clipboard?
How do I read text from the clipboard? - AskPython
February 27, 2023 - pyperclip is a Python module that provides a simple and cross-platform way to access the clipboard in order to read or write text content. It allows developers to easily copy and paste text between different applications or within the same ...
🌐
PyPI
pypi.org › project › klembord
klembord · PyPI
klembord is a python 3 package that provides full clipboard access on supported platforms (Linux and Windows for now, though this may change in the future).
      » pip install klembord
    
Published   Dec 29, 2021
Version   0.3.0
Find elsewhere
🌐
PyPI
pypi.org › project › pyperclip
pyperclip · PyPI
On Mac, this module makes use of the pbcopy and pbpaste commands, which should come with the os. On Linux, this module makes use of the xclip or xsel commands, which should come with the os.
      » pip install pyperclip
    
Published   Sep 26, 2025
Version   1.11.0
🌐
GitHub
github.com › terryyin › clipboard
GitHub - terryyin/clipboard: A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux.
A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux. - terryyin/clipboard
Starred by 89 users
Forked by 6 users
Languages   Python 86.7% | Makefile 13.3% | Python 86.7% | Makefile 13.3%
🌐
Python GTK+ 3 Tutorial
python-gtk-3-tutorial.readthedocs.io › en › latest › clipboard.html
20. Clipboard — Python GTK+ 3 Tutorial 3.4 documentation
1import gi 2 3gi.require_version("Gtk", "3.0") 4from gi.repository import Gtk, Gdk 5 6 7class ClipboardWindow(Gtk.Window): 8 def __init__(self): 9 super().__init__(title="Clipboard Example") 10 11 grid = Gtk.Grid() 12 13 self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) 14 self.entry = Gtk.Entry() 15 self.image = Gtk.Image.new_from_icon_name("process-stop", Gtk.IconSize.MENU) 16 17 button_copy_text = Gtk.Button(label="Copy Text") 18 button_paste_text = Gtk.Button(label="Paste Text") 19 button_copy_image = Gtk.Button(label="Copy Image") 20 button_paste_image = Gtk.Button(label="Paste
🌐
Note.nkmk.me
note.nkmk.me › home › python
Copy and Paste Text to the Clipboard with pyperclip in Python | note.nkmk.me
January 30, 2024 - Pyperclip can be installed using the pip command (or pip3 depending on the environment). ... On Linux, you'll need the xclip or xsel command (installed via apt, etc.), and the gtk or PyQt4 modules (installed via pip).
🌐
Reddit
reddit.com › r/linux4noobs › how to copy to the clipboard using python?
r/linux4noobs on Reddit: How to copy to the clipboard using python?
December 15, 2015 -

I'm using centos 7, python3.5

Doing how to Automate the boring stuff.

The book uses pyperclip, I downloaded it using:

pip install pyperclip

It seems i am missing some modules: PyQt4 and gtk

pip doesn't recognize them:

pip install gtk

pip install PyQt4

Could not find a version that satisfies the requirement gtk (from versions: )
No matching distribution found for gtk

I'm guessing I'm also missing some dependencies, but pip should still recognize these packages according to this pdf on page 7

https://media.readthedocs.org/pdf/pyperclip/latest/pyperclip.pdf

any suggestions?

thanks.

EDIT: Thanks for the replies

I was finally able to download xclip from here : http://pkgs.repoforge.org/xclip/

after installation, pyperclip is working great.

🌐
Medium
medium.com › analytics-vidhya › clipboard-operations-in-python-3cf2b3bd998c
Clipboard operations in python.. It is very easy to perform copy/paste… | by Keerti Prajapati | Analytics Vidhya | Medium
January 22, 2024 - (These commands should come with OS X.). On Linux, install xclip, xsel, or wl-clipboard (for “wayland” sessions) via package manager. For example, in Debian: sudo apt-get install xclip sudo apt-get install xsel sudo apt-get install wl-clipboard
🌐
Reddit
reddit.com › r/python › copykitten: the missing clipboard library for python
r/Python on Reddit: copykitten: the missing clipboard library for Python
February 21, 2024 -

What My Project Does

copykitten is a clipboard library with support for text and images and a simple API. It's built around Rust arboard library. Thanks to this, it's multiplatform and doesn't require any dependencies.

Target Audience

Developers building CLI/GUI/TUI applications. The library has beta status on PyPI, but the underlying Rust library is pretty stable, being used in commercial projects like Bitwarden and 1Password.

Comparison

There are lots of other clipboard libraries for Python: pyperclip, jaraco.clipboard, pyclip, just to name a few. However, most of them are not maintained for years and require the presence of additional libraries or tools in the operating system. copykitten doesn't suffer from these shortcomings.

A bit of history

Throughout my years with Python there were several times when I needed to use the clipboard in my applications and every time I had to fall back to some shaky methods like asking the end user to install xclip and calling subprocess.run. This never felt great.

Right now I'm making a multiplayer TUI game (maybe I’ll showcase it later too :) ), where users can copy join game codes into the clipboard to easily share it (much like Among Us). This is how I came to the idea of making such a library. I also wanted to try Rust for a long time, and so this all just clicked in my head instantly.

I had fun building it and definitely had some pain too and learned a bit of nitty-gritty details about how clipboards work in different operating systems. Now I hate Windows.

With this post I hope to gain some attention to the project so that I can receive feedback about the issues and maybe feature requests and spread the word that there's a modern, convenient alternative to the existing packages.

Feel free to try it out: https://github.com/Klavionik/copykitten

🌐
Readthedocs
pyperclip.readthedocs.io › en › latest
Welcome to Pyperclip’s documentation! — Pyperclip 1.5 documentation
Currently, this error should only appear on Linux (not Windows or Mac). You can fix this by installing one of the copy/paste mechanisms: sudo apt-get install xsel to install the xsel utility.
🌐
Fly.io
community.fly.io › python
How to implement copying to the clipboard in Python? - Python - Fly.io
February 8, 2025 - I don’t understand how it is possible to implement copying to the clipboard on the server. I run the FLET application, everything works locally, but when I test it on the server, it doesn’t work. I tested both pyperclip and page.set_clipboard - no result.
🌐
Homeip
dirkmittler.homeip.net › blog › archives › 6928
Example Python code, that saves text to the Linux clipboard, persistently. | Dirk Mittler's Blog
April 8, 2019 - However, left-clicking on one of the entries in the Klipper History will cause the ‘Clipboard’ X11 pointer to point to it, unless that just happens to be the most-recent entry. Basically, the user community wanted an alternative to Windows, that has familiar features, and instead, the Linux developers left them a well-hidden Easter Egg. (:1) I recently needed to install a Python script, which hashes a domain-name, password combination, and which has as feature the ability to save the hash-code ‘to the clipboard’, instead of just printing it out, so that the user should next be able to paste the hash-code, and in some cases do so, without the hash-code ever being displayed.
🌐
Clay-Technology World
clay-atlas.com › home › [python] use subprocess module to access system clipboard
[Python] Use Subprocess Module to Access System Clipboard - Clay-Technology World
December 28, 2021 - In Linux, we can use ... The following is the xclip parameter description. -selection: Since there are multiple clipboards in the system, we need to select the clipboard to be used.
🌐
Sfriederichs
sfriederichs.github.io › how-to › python3 › clipboard › 2020 › 07 › 14 › Python-Clipboard.html
How-to Access the Windows Clipboard with Python
July 14, 2020 - This will be done on a Windows 10 PC with Python 3 (3.8.1 specifically). It seems that this is not nearly as straightforward as I thought it might be. This seems largely to do with the fact that I want to do it on Windows as opposed to (possibly) Linux. The first result from Google forwards me to a big ol’ list of things I can try which seem somewhat involved. I don’t really want to install TKinter in order to just get the clipboard...