See Pyperclip. Example (taken from Pyperclip site):

import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()

Also, see Xerox. But it appears to have more dependencies.

Answer from robert on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Copy to system clipboard - Python Help - Discussions on Python.org
June 2, 2022 - Right now, the hash value is displayed in a Text Widget, which is has the ‘state’ set to ‘disabled’, so that said value can’t be messed with. I’d like to be able to copy that to the system clipboard without using yet another dependency. I’ve seen some methods posted in a number ...
Discussions

How to implement copying to the clipboard in Python?
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. More on community.fly.io
🌐 community.fly.io
0
0
February 8, 2025
How to copy selected text to clipboard?
pyperclip.paste() will paste whatever you have copied to the clipboard. You don't need to use pyperclip.copy() first. You can set it to a variable too. copied_text = pyperclip.paste() More on reddit.com
🌐 r/learnpython
4
3
June 17, 2018
python - How do I copy a string to the clipboard? - Stack Overflow
I have tested characters ... with Python 3.5 and pyperclip 1.5.27. 2016-07-03T15:55:54.75Z+00:00 ... pyperclip also works on Mac and Linux too (not just Windows), which is nice. 2021-03-04T15:39:04.52Z+00:00 ... You can use the excellent pandas, which has a built in clipboard support, but you need to pass through a DataFrame and call to_clipboard. import pandas as pd df = pd.DataFrame(['Text to copy']) ... More on stackoverflow.com
🌐 stackoverflow.com
Script to copy lines of text?
You can use string.split to split your string (you read from somewhere) at the dots. But I think any further automation (which is certainly possible) depends heavily what exactly the task is. But one thing that certainly would work is hotkeys for pasting / loading the next sentence into the clipboard. For hotkeys I'd use pynput, the clipboard access depends on the OS. More on reddit.com
🌐 r/learnpython
13
4
June 30, 2020
🌐
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 - Use pyperclip.paste() to paste (get) text from the clipboard. pyperclip.copy('text to be copied') print(pyperclip.paste()) # text to be copied print(type(pyperclip.paste())) # <class 'str'>
🌐
Delft Stack
delftstack.com › home › howto › python › python copy to clipboard
How to Copy Text to Clipboard in Python | Delft Stack
February 12, 2024 - The copy() function is then called with this text, placing it on the clipboard. Next, the paste() function is used to retrieve the text from the clipboard and store it in the variable pasted_text.
🌐
GitHub
gist.github.com › adam-p › 4173174
Python function to copy text to clipboard (so far only supports Windows). · GitHub
I was able to use python 3.6.5 using Windows 10 64 bit with this, but I had to add encoding='utf8' as an argument to Popen to pass strings to the function rather than a byte array. I think you can also easily extend this to macOS using pbcopy in place of clip as well. ... You might want to take a look (or contribute to) the pyperclip project, which does copy/paste functions for Windows/Mac/Linux.
🌐
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…
🌐
IncludeHelp
includehelp.com › python › copy-data-to-clipboard.aspx
Copy data to clipboard in Python
To copy the text to the clipboard, pass a string to pyperclip.copy() and to paste the text from the clipboard, invoke pyperclip.paste() and the copied text will be returned as a string value.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how to copy selected text to clipboard?
r/learnpython on Reddit: How to copy selected text to clipboard?
June 17, 2018 -

How can I select text and copy it to the clipboard?

The selection part actually works. Either with "ctrl+a" or a triple left mouseclick. But I can't "ctrl+c" it into the clipboard.

I tried it with pyautogui like a normal user would, but it doesn't work.

pyautogui.keyDown('ctrl')

pyautogui.keyDown('c')

pyautogui.keyUp('c')

pyautogui.keyUp('ctrl')

Pyperclip works to read out the content of the clipboard, but it seems like it can only copy strings to the clipbard, that need to be added manually like this:

Pyperclip.copy('test')

But I need to copy my text selection to the clipboard.

edit: if you know any other way to store selected text in a variable, that would be alright too

Top answer
1 of 16
390

Actually, pywin32 and ctypes seem to be an overkill for this simple task. tkinter is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.

If all you need is to put some text to system clipboard, this will do it:

from tkinter import Tk # in Python 2, use "Tkinter" instead 
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.update() # now it stays on the clipboard after the window is closed
r.destroy()

And that's all, no need to mess around with platform-specific third-party libraries.

If you are using Python 2, replace tkinter with Tkinter.

2 of 16
110

I didn't have a solution, just a workaround.

Windows Vista onwards has an inbuilt command called clip that takes the output of a command from command line and puts it into the clipboard. For example, ipconfig | clip.

So I made a function with the os module which takes a string and adds it to the clipboard using the inbuilt Windows solution.

import os
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

# Example
addToClipBoard('penny lane')

# Penny Lane is now in your ears, eyes, and clipboard.

As previously noted in the comments however, one downside to this approach is that the echo command automatically adds a newline to the end of your text. To avoid this you can use a modified version of the command:

def addToClipBoard(text):
    command = 'echo | set /p nul=' + text.strip() + '| clip'
    os.system(command)

If you are using Windows XP it will work just following the steps in Copy and paste from Windows XP Pro's command prompt straight to the Clipboard.

🌐
PyPI
pypi.org › project › clipboard
clipboard · PyPI
Well, as I was trying to implement this, I realize that everything is included in pyperclip already: https://pypi.python.org/pypi/pyperclip/ But anyway, clipboard is a better name. You are free to choose:-) I might add more features to it. Usage: import clipboard clipboard.copy("abc") # now the clipboard content will be string "abc" text = clipboard.paste() # text will have the content of clipboard ·
      » pip install clipboard
    
Published   May 22, 2014
Version   0.0.4
🌐
Python Forum
python-forum.io › thread-24784.html
How to copy data to clipboard
May 14, 2021 - I am trying to copy data from my Python program to the clipboard so that I can paste it into a spreadsheet for graphing and display. I tried using pyperclip, but this only works with simple data types like strings. My data consists of tables (ie li...
🌐
GeeksforGeeks
geeksforgeeks.org › pyperclip-module-in-python
Pyperclip module in Python - GeeksforGeeks
February 27, 2020 - Installing pyperclip: pip install pyperclip 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.
🌐
Codingem
codingem.com › home › how to copy text to clipboard in python
How to Copy Text to Clipboard in Python - codingem.com
October 15, 2022 - To copy text to clipboard in Python, use the pyperclip module's copy() function. For example, pyperclip.copy("Some text").
🌐
Automate the Boring Stuff
automatetheboringstuff.com › 2e › chapter6
Chapter 6 – Manipulating Strings
Or you could automate this task with a short Python script. The bulletPointAdder.py script will get the text from the clipboard, add a star and space to the beginning of each line, and then paste this new text to the clipboard. For example, if I copied the following text (for the Wikipedia article “List of Lists of Lists”) 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 ...
🌐
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 copy_clipboard('This is a test')
🌐
Omz Software
omz-software.com › pythonista › docs › ios › clipboard.html
clipboard — Copy and paste — Python 3.6.1 documentation
February 19, 2020 - The clipboard module defines two simple functions to read and write to the text clipboard (a.k.a.
🌐
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 - Clipboard operations in python. It is very easy to perform copy/paste operations of Clipboard using ctrl+c and ctrl+v , you may think that performing clipboard operations using programming language …
🌐
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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_clipboard.html
pandas.DataFrame.to_clipboard — pandas 3.0.1 documentation
>>> df.to_clipboard(sep=",", index=False) ... # Wrote the following to the system clipboard: ... # A,B,C ... # 1,2,3 ... # 4,5,6 · Using the original pyperclip package for any string output format. import pyperclip html = df.style.to_html() pyperclip.copy(html)