271

I'm trying to make a basic Windows application that builds a string out of user input and then adds it to the clipboard. How do I copy a string to the clipboard using Python?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
tester
  • 22,441
  • 25
  • 88
  • 128
  • 4
    Related to [this question](http://stackoverflow.com/q/101128/296974). – glglgl Nov 15 '12 at 13:48
  • jaraco.clipboard does it (too): clipboard.copy(variable) – JinSnow Nov 23 '16 at 15:54
  • Also see my [answer](https://stackoverflow.com/questions/24559675/python-copying-from-clipboard-using-tkinter-without-displaying-window/25594653#25594653) to a related question about copying to the clipboard using Tkinter without showing a window. It includes a Python function that replaces/returns clipboard text using Tkinter. – Edward Oct 22 '17 at 13:32
  • For rich text, [windows - Accessing alternate clipboard formats from python - Stack Overflow](https://stackoverflow.com/questions/32959928/accessing-alternate-clipboard-formats-from-python) has some pointers. – user202729 Aug 23 '23 at 08:56

29 Answers29

371

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
atomizer
  • 4,458
  • 1
  • 17
  • 9
  • 65
    Get contents of clipboard: result = r.selection_get(selection = "CLIPBOARD") – Michael Allan Jackson Jul 13 '11 at 03:19
  • 39
    @SurDin Tkinter was renamed tkinter in python 3, so it depends on what version you're using. – Honest Abe Mar 27 '12 at 00:53
  • 1
    Cool solution, but I'm having some trouble with Unicode strings, know of any workarounds (like, convert unicode chars to an ascii equivalent)? – kapace May 23 '12 at 17:18
  • 2
    @kapace: [related bug in Python](http://bugs.python.org/issue14777). It is fixed now. – jfs Sep 19 '12 at 13:32
  • 30
    all my apps get unresponsive after pasting the contents of the clipboard with this function, strangely, getting the result works fine. – Bartlomiej Lewandowski Feb 02 '13 at 20:53
  • 3
    @BartlomiejLewandowski as far as I can tell tkinter wont release the clipboard while you are looping anything in python. – hamsolo474 - Reinstate Monica Apr 23 '13 at 15:57
  • 21
    It works if I don't call `r.destroy()`. Once I call that, the clipboard becomes empty and pressing Ctrl-V may cause the target app to freeze. (OS: Windows 7 x64) – netvope Aug 24 '13 at 03:45
  • 1
    Whe used in a command line program, this will automatically make the target app freeze (Windows 7) untill I call `r.update()` or the whole script is over. – PhilMacKay Mar 06 '14 at 16:55
  • 5
    Get contents of clipboard: `r.clipboard_get()` – Kiwi May 03 '14 at 22:48
  • 1
    After the program ends, this keeps the clipboard on my Windows 7 but not on Windows 8.1, unless calling `win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)`. – Cees Timmerman Dec 04 '14 at 09:14
  • 3
    If you use this in a console script this might lead to an error, that the .destroy() function will not work ("can't invoke "event" command: application has been destroyed while executing [...]"). To prevent this, call `r.update()` before `r.destroy`. – user136036 Jan 17 '15 at 01:11
  • 2
    This doesn't work at all for me using Python 2.7.2 in IDLE. the r.clipboard_get() posted by @Kiwi prints it out to the IDLE Python shell but it isn't available in other applications (tried posting into notepad etc.) – jbchurchill May 28 '15 at 13:29
  • 8
    To me this doesn't work for python 3.4, but it works for python 2.7 (yes, with tkinter instead of Tkinter) – Matty Oct 01 '15 at 15:58
  • 2
    This works on Linux / python 2.7.6 in an interactive console, but not from a script!? I tried a few tests, impossible to understand why... – calandoa Oct 13 '15 at 14:35
  • is it possible to send back the data as CF_HTML using Tkinter? – JinSnow Jan 16 '17 at 21:25
  • 2
    In Spyder, this only works in the Python console but not in the i-Python console. – Joooeey May 16 '17 at 21:40
  • This seems to blow it up if it's appending mac address in plain text. – Sometowngeek Mar 31 '18 at 18:53
  • 5
    ctypes (a minimalist wrapper around the low-level Win32 API) is overkill, so people should use *TK*? This answer is very, very confused... – Glenn Maynard Apr 16 '18 at 22:36
  • This seems to be slower than pywin32 – Seaky Lone Jan 20 '19 at 21:16
  • 10
    This is a good example for a very old outdated answer clinging on top, while you have to scroll down, looking at the “answered” dates to find the *currently* best way to do it. – Torsten Bronger May 07 '21 at 08:11
  • The pyperclip module below worked for me. The above script rendered apps unresponsive. – Jack Oct 14 '21 at 19:32
98

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.

user1227883
  • 1,037
  • 7
  • 3
  • is there any limitations on the length of the text? – stanleyxu2005 Jun 11 '12 at 07:28
  • 34
    what happens if `text` contains `| calc.exe `? – Willi Ballenthin Jan 18 '13 at 19:36
  • 2
    @WilliBallenthin then you need to wrap it in double quotes. But what if it CONTAINS double quotes? Then you need to double the double quotes. `text with " quotes and | pipe` becomes `"text with "" quotes and | pipe"` Although this may have problems on systems with windows older than 95. – ColBeseder May 27 '13 at 14:52
  • Your solution seem sperfect! Now I just need to find a way to pass tabs `"\t"`... All other solution using a GUI library will freeze untill a GUI event happens. – PhilMacKay Mar 06 '14 at 17:20
  • 12
    Extremely insecure function... Content sent to your clipboard is now an entry vector and thus increases your attack surface. – Phil L. Aug 21 '14 at 12:13
  • 3
    I also need support for newlines, so I modified this to use `type`. I write my text to file, and use the command `type myfile.txt | clip`. – Mixopteryx Jan 26 '16 at 14:06
  • 1
    Although this is the best answer to me, it has an issue (Python 3.5, Windows 10); an unwanted newline is always added at the end of the string. How to avoid it? – mmj Dec 10 '16 at 10:55
  • This is extremely weak to injection. Please never use this if the user has any input in what's to be copied that allows for actual text to be entered. – J. Doe Jun 15 '18 at 01:25
  • How can you empty the clipboard? If you run it twice, the new text is copied after the one that was already copied. – Quaerendo Jul 03 '20 at 11:26
  • 1
    Building on @Mixopteryx "write to file" solution, you could also use this variation `clip < myfile.txt`. – Ray Li Aug 08 '20 at 14:55
  • Instead of using os.system, use subprocess.run, which gives you more control over stdin. For example `subprocess.run(['clip'], stdin=open(somefile,'rb'))` – Paul Du Bois Apr 16 '21 at 19:47
62

You can use pyperclip - cross-platform clipboard module. Or Xerox - similar module, except requires the win32 Python module to work on Windows.

Flimm
  • 136,138
  • 45
  • 251
  • 267
pongo
  • 1,335
  • 13
  • 11
  • `pyperclip` doesn't do Unicode on Windows. `win32clipboard` does. – Cees Timmerman Dec 03 '14 at 17:29
  • 18
    My `pyperclip` patch was accepted; `c:\python34\Scripts\pip install --upgrade pyperclip` to handle Unicode text. – Cees Timmerman Dec 05 '14 at 14:02
  • 1
    It took me a while to find out that this is `pyperclip`, not `paperclip`. Also, as on 2016, pyperclip works with Unicode characters too. I have tested characters ±°©©αβγθΔΨΦåäö to work on Win10 64-bit, with Python 3.5 and pyperclip 1.5.27. – Niko Föhr Jul 03 '16 at 15:55
  • 1
    pyperclip also works on Mac and Linux too (not just Windows), which is nice. – David Foster Mar 04 '21 at 15:39
57

The simplest way is with pyperclip. Works in python 2 and 3.

To install this library, use:

pip install pyperclip

Example usage:

import pyperclip

pyperclip.copy("your string")

If you want to get the contents of the clipboard:

clipboard_content = pyperclip.paste()
maviz
  • 2,392
  • 1
  • 11
  • 11
  • `pyperclip.paste()` does not work with images just returns `NoneType` error. but works with right click and copy then using python to paste the copied results. – JayRizzo Sep 09 '19 at 23:38
  • 2
    @JayRizzo the question doesn't ask about copying an image to the clipboard. However, you could use the ImageGrab.grabclipboard() module of the Pillow library (pip install Pillow). – maviz Jan 20 '21 at 06:14
47

You can use the excellent pandas, which has a built in clipboard support, but you need to pass through a DataFrame.

import pandas as pd
df=pd.DataFrame(['Text to copy'])
df.to_clipboard(index=False,header=False)
Gadi Oron
  • 880
  • 8
  • 6
  • 4
    +1. I like this solution. It's even simpler than the accepted Tkinter solution. If you imported pandas anyways, there is no additional overhead. It also works across all platforms. It does not even (in most cases) require installing a new package. – ChaimG Feb 02 '17 at 18:04
  • 6
    this uses `pyperclip` anyway, so better use `pyperpclip` – maxbellec Jun 13 '17 at 08:02
  • 5
    For most people (i.e. me), `pandas` is readily available but `import pyperclip` doesn't work. So I don't agree with "better use pyperclip". – Doctor Core Jun 25 '19 at 09:16
  • 8
    If you're restricted to using just pandas, you could use pyperclip directly through `import pandas.io.clipboard as pyperclip` or name it whatever you want. That's where it sits within `pandas`, at least – David Culbreth Oct 29 '19 at 18:36
  • 1
    This seems to add a newline character to the string you copy – Gijs van Oort May 14 '20 at 08:02
  • This is by far the simplest and most elegant solution for this problem. Thank you very much for sharing this. – Sabito stands with Ukraine Jun 08 '20 at 07:02
  • pandas is soooo goooood – Joao Vitor Deon Nov 11 '20 at 15:08
  • 2
    If you already have `pandas` loaded and you don't want to keep the dataframe, this can be reduced to a single line: `pd.DataFrame(['Text to copy']).to_clipboard(excel=False,index=False,header=False)` The `excel=False` parameter removes the newline mentioned in the comment from @GijsvanOort – scign Aug 26 '21 at 17:22
44

You can also use ctypes to tap into the Windows API and avoid the massive pywin32 package. This is what I use (excuse the poor style, but the idea is there):

import ctypes

# Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
OpenClipboard = ctypes.windll.user32.OpenClipboard    # Basic clipboard functions
EmptyClipboard = ctypes.windll.user32.EmptyClipboard
GetClipboardData = ctypes.windll.user32.GetClipboardData
SetClipboardData = ctypes.windll.user32.SetClipboardData
CloseClipboard = ctypes.windll.user32.CloseClipboard
GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc    # Global memory allocation
GlobalLock = ctypes.windll.kernel32.GlobalLock     # Global memory Locking
GlobalUnlock = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000

def Get():
  OpenClipboard(None) # Open Clip, Default task

  pcontents = GetClipboardData(1) # 1 means CF_TEXT.. too lazy to get the token thingy...

  data = ctypes.c_char_p(pcontents).value

  #GlobalUnlock(pcontents) ?
  CloseClipboard()

  return data

def Paste(data):
  OpenClipboard(None) # Open Clip, Default task

  EmptyClipboard()

  hCd = GlobalAlloc(GMEM_DDESHARE, len(bytes(data,"ascii")) + 1)

  pchData = GlobalLock(hCd)

  strcpy(ctypes.c_char_p(pchData), bytes(data, "ascii"))

  GlobalUnlock(hCd)

  SetClipboardData(1, hCd)

  CloseClipboard()
Community
  • 1
  • 1
kapace
  • 457
  • 7
  • 12
  • 5
    At least in python 2.6 x64, I had to change `bytes(data,"ascii")` to `bytes(data)`. Thanks for answering the question, I can't use pywin32 or tk or a number of other things and this works. – Pat C Feb 08 '13 at 21:37
  • 1
    No worries, but note that the data returned from the clipboard is actually in another encoding, I believe it is Windows CP-1252. This was somewhat hacked together, but if you don't use the correct encoding then non-ascii characters will raise an error or decode incorrectly. – kapace Feb 13 '13 at 05:33
  • Variable names should not need comments, and everything should support Unicode. – Cees Timmerman Dec 04 '14 at 14:03
  • `bytes(data, "mbcs")` will work with windows default encoding. Allowed me to load this to the clipboard `"másreas ç saod é í ó u* ü ö ï/"` and read it back correctly. – mvbentes May 12 '16 at 17:18
  • 2
    using mbcs gives me this: OSError: exception: access violation writing 0x0000000000000000 – Seaky Lone Jan 20 '19 at 21:59
42

Here's the most easy and reliable way I found if you're okay depending on Pandas. However I don't think this is officially part of the Pandas API so it may break with future updates. It works as of 0.25.3

from pandas.io import clipboard
clipboard.copy("test")
pyjamas
  • 4,608
  • 5
  • 38
  • 70
  • 2
    Works for me, except I might use `from pandas.io import clipboard` then `clipboard.copy(...)` to avoid confusion with other `copy` methods. – nekomatic Jul 29 '20 at 19:47
  • 4
    that is just a wrapper around pyperclip! (or to be more precise... pandas simply puts a copy of some pyperclip version into their src ) – raphael Jun 29 '22 at 11:16
12

For some reason I've never been able to get the Tk solution to work for me. kapace's solution is much more workable, but the formatting is contrary to my style and it doesn't work with Unicode. Here's a modified version.

import ctypes

from ctypes.wintypes import BOOL, HWND, HANDLE, HGLOBAL, UINT, LPVOID
from ctypes import c_size_t as SIZE_T

OpenClipboard = ctypes.windll.user32.OpenClipboard
OpenClipboard.argtypes = HWND,
OpenClipboard.restype = BOOL
EmptyClipboard = ctypes.windll.user32.EmptyClipboard
EmptyClipboard.restype = BOOL
GetClipboardData = ctypes.windll.user32.GetClipboardData
GetClipboardData.argtypes = UINT,
GetClipboardData.restype = HANDLE
SetClipboardData = ctypes.windll.user32.SetClipboardData
SetClipboardData.argtypes = UINT, HANDLE
SetClipboardData.restype = HANDLE
CloseClipboard = ctypes.windll.user32.CloseClipboard
CloseClipboard.restype = BOOL
CF_UNICODETEXT = 13

GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc
GlobalAlloc.argtypes = UINT, SIZE_T
GlobalAlloc.restype = HGLOBAL
GlobalLock = ctypes.windll.kernel32.GlobalLock
GlobalLock.argtypes = HGLOBAL,
GlobalLock.restype = LPVOID
GlobalUnlock = ctypes.windll.kernel32.GlobalUnlock
GlobalUnlock.argtypes = HGLOBAL,
GlobalSize = ctypes.windll.kernel32.GlobalSize
GlobalSize.argtypes = HGLOBAL,
GlobalSize.restype = SIZE_T

GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040

unicode_type = type(u'')

def get():
    text = None
    OpenClipboard(None)
    handle = GetClipboardData(CF_UNICODETEXT)
    pcontents = GlobalLock(handle)
    size = GlobalSize(handle)
    if pcontents and size:
        raw_data = ctypes.create_string_buffer(size)
        ctypes.memmove(raw_data, pcontents, size)
        text = raw_data.raw.decode('utf-16le').rstrip(u'\0')
    GlobalUnlock(handle)
    CloseClipboard()
    return text

def put(s):
    if not isinstance(s, unicode_type):
        s = s.decode('mbcs')
    data = s.encode('utf-16le')
    OpenClipboard(None)
    EmptyClipboard()
    handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len(data) + 2)
    pcontents = GlobalLock(handle)
    ctypes.memmove(pcontents, data, len(data))
    GlobalUnlock(handle)
    SetClipboardData(CF_UNICODETEXT, handle)
    CloseClipboard()

paste = get
copy = put

The above has changed since this answer was first created, to better cope with extended Unicode characters and Python 3. It has been tested in both Python 2.7 and 3.5, and works even with emoji such as \U0001f601 ().

Update 2021-10-26: This was working great for me in Windows 7 and Python 3.8. Then I got a new computer with Windows 10 and Python 3.10, and it failed for me the same way as indicated in the comments. This post gave me the answer. The functions from ctypes don't have argument and return types properly specified, and the defaults don't work consistently with 64-bit values. I've modified the above code to include that missing information.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • @CeesTimmerman I'd love to have a Windows 8.1 system to test why. I might have a chance to investigate later today. Are you sure you had text in the clipboard? – Mark Ransom Dec 04 '14 at 13:42
  • Yes. I've extensively tested clipboard code in Python over the past three days. – Cees Timmerman Dec 04 '14 at 13:44
  • The `put()` function also needs work; emoji "" (\U0001f400) is copied as "" (\U0001f4cb), or "." turns to "". – Cees Timmerman Dec 05 '14 at 09:20
  • @CeesTimmerman that explains it. The internal Unicode representation changed, I can't remember if it was 3.3 or 3.4. To fix it will require an explicit encoding to UTF-16. It's not a bug. – Mark Ransom Dec 05 '14 at 13:39
  • 3.3, and `len(data.encode('utf-16-le')) + 2` fixed it. – Cees Timmerman Dec 05 '14 at 13:44
  • I get "NameError: name 'unicode' is not defined. Either I'm overlooking something basic, or this script is missing this variable. – Yngvar Kristiansen Jan 21 '16 at 08:22
  • @YngvarKristiansen it's a Python 2 vs. Python 3 thing. This script needs some work for Python 3, I'll try to fix it later. – Mark Ransom Jan 21 '16 at 14:58
  • 1
    @YngvarKristiansen I finally got around to making those changes. I'm confident now that this code works for most modern versions of Python and every possible Unicode character. – Mark Ransom Jun 03 '16 at 05:20
  • @CeesTimmerman I made this clipboard code a lot more robust this summer, not sure if you had a chance to see those changes. – Mark Ransom Nov 18 '16 at 15:41
  • ctypes.memmove(pcontents, data, len(data)) gives an error: OSError: exception: access violation writing 0x0000000000000000 – Seaky Lone Jan 20 '19 at 21:49
  • @SeakyLone that probably means there was no text on the clipboard. – Mark Ransom Jan 20 '19 at 22:11
  • @MarkRansom Thanks for replying. I just copied some text and called your function again. The text was removed from the clipboard. I commented out the EmptyClipboard(). The text stays but still the same error. Any idea how to fix it? – Seaky Lone Jan 20 '19 at 22:18
  • @SeakyLone my bad, I thought you were trying to read from the clipboard not write to it. Your error indicates that either `GlobalAlloc` or `GlobalLock` failed, you'd need to print out `handle` to know which one. It's hard to see how either could fail, they're very basic Windows functions. – Mark Ransom Jan 20 '19 at 23:43
  • @MarkRansom The handle/GlobalAlloc varies and is a very large negative number. And the pcontentsGlobalLock is always 0. FYI, I am using Windows 10 + Python 3.7. – Seaky Lone Jan 21 '19 at 02:42
  • @SeakyLone then apparently it's `GlobalLock` that's failing. I can't tell you why that would be. – Mark Ransom Jan 21 '19 at 05:03
  • @SeakyLone I just updated the code with a fix. It may be incomplete though, it just occurred to me that `memmove` would require a similar fix. – Mark Ransom Oct 27 '21 at 02:48
12

Use pyperclip module

Install using pip pip install pyperclip.

Copy text "Hello World!" to clip board

import pyperclip
pyperclip.copy('Hello World!')

You can use Ctrl+V anywhere to paste this somewhere.

Paste the copied text using python

pyperclip.paste() # This returns the copied text of type <class 'str'>
Ali Sajjad
  • 3,589
  • 1
  • 28
  • 38
11

I've tried various solutions, but this is the simplest one that passes my test:

#coding=utf-8

import win32clipboard  # http://sourceforge.net/projects/pywin32/

def copy(text):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
    win32clipboard.CloseClipboard()
def paste():
    win32clipboard.OpenClipboard()
    data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
    win32clipboard.CloseClipboard()
    return data

if __name__ == "__main__":  
    text = "Testing\nthe “clip—board”: "
    try: text = text.decode('utf8')  # Python 2 needs decode to make a Unicode string.
    except AttributeError: pass
    print("%r" % text.encode('utf8'))
    copy(text)
    data = paste()
    print("%r" % data.encode('utf8'))
    print("OK" if text == data else "FAIL")

    try: print(data)
    except UnicodeEncodeError as er:
        print(er)
        print(data.encode('utf8'))

Tested OK in Python 3.4 on Windows 8.1 and Python 2.7 on Windows 7. Also when reading Unicode data with Unix linefeeds copied from Windows. Copied data stays on the clipboard after Python exits: "Testing the “clip—board”: "

If you want no external dependencies, use this code (now part of cross-platform pyperclip - C:\Python34\Scripts\pip install --upgrade pyperclip):

def copy(text):
    GMEM_DDESHARE = 0x2000
    CF_UNICODETEXT = 13
    d = ctypes.windll # cdll expects 4 more bytes in user32.OpenClipboard(None)
    try:  # Python 2
        if not isinstance(text, unicode):
            text = text.decode('mbcs')
    except NameError:
        if not isinstance(text, str):
            text = text.decode('mbcs')
    d.user32.OpenClipboard(0)
    d.user32.EmptyClipboard()
    hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2)
    pchData = d.kernel32.GlobalLock(hCd)
    ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text)
    d.kernel32.GlobalUnlock(hCd)
    d.user32.SetClipboardData(CF_UNICODETEXT, hCd)
    d.user32.CloseClipboard()

def paste():
    CF_UNICODETEXT = 13
    d = ctypes.windll
    d.user32.OpenClipboard(0)
    handle = d.user32.GetClipboardData(CF_UNICODETEXT)
    text = ctypes.c_wchar_p(handle).value
    d.user32.CloseClipboard()
    return text
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
  • Where do you get `win32clipboard`? It's not part of my Python 2.7. And why does `paste` use `CF_TEXT` instead of `CF_UNICODETEXT`? – Mark Ransom Dec 04 '14 at 15:42
  • @MarkRansom pywin32, and because my test worked fine until i made it harder using . I've updated the code. – Cees Timmerman Dec 04 '14 at 15:57
10

If you don't like the name you can use the derivative module clipboard.

Note: It's just a selective wrapper of pyperclip

After installing, import it:

import clipboard

Then you can copy like this:

clipboard.copy("This is copied")

You can also paste the copied text:

clipboard.paste()
Oskar Austegard
  • 4,599
  • 4
  • 36
  • 50
Nouman
  • 6,947
  • 7
  • 32
  • 60
  • 3
    This seems like the best solution. clipboard can be installed with `pip install clipboard`. – vy32 Mar 27 '20 at 17:50
  • 5
    The `clipboard` package is just a shameless copy of the `pyperclip` package. As indicated by its code [here](https://github.com/terryyin/clipboard/blob/master/clipboard.py). – jdhao Feb 03 '21 at 08:25
  • its a good solution for string. What about bytes ? any solution like this to copy bytes to clipboard in single line of python – Willy satrio nugroho Nov 16 '21 at 04:49
9

Looks like you need to add win32clipboard to your site-packages. It's part of the pywin32 package

Jason Coon
  • 17,601
  • 10
  • 42
  • 50
7

Not all of the answers worked for my various python configurations so this solution only uses the subprocess module. However, copy_keyword has to be pbcopy for Mac or clip for Windows:

import subprocess
subprocess.run('copy_keyword', universal_newlines=True, input='New Clipboard Value ')

Here's some more extensive code that automatically checks what the current operating system is:

import platform
import subprocess

copy_string = 'New Clipboard Value '

# Check which operating system is running to get the correct copying keyword.
if platform.system() == 'Darwin':
    copy_keyword = 'pbcopy'
elif platform.system() == 'Windows':
    copy_keyword = 'clip'

subprocess.run(copy_keyword, universal_newlines=True, input=copy_string)
Fetchinator7
  • 106
  • 2
  • 4
4

I think there is a much simpler solution to this.

name = input('What is your name? ')
print('Hello %s' % (name) )

Then run your program in the command line

python greeter.py | clip

This will pipe the output of your file to the clipboard

jdeyrup
  • 1,114
  • 1
  • 12
  • 13
3

Widgets also have method named .clipboard_get() that returns the contents of the clipboard (unless some kind of error happens based on the type of data in the clipboard).

The clipboard_get() method is mentioned in this bug report:
http://bugs.python.org/issue14777

Strangely, this method was not mentioned in the common (but unofficial) online TkInter documentation sources that I usually refer to.

mpb
  • 1,277
  • 15
  • 18
2

This is an improved answer of atomizer. Note that

  • there are 2 calls of update() and
  • inserted 200 ms delay between them.

They protect freezing applications due to an unstable state of the clipboard:

from Tkinter import Tk
import time     

r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('some string')

r.update()
time.sleep(.2)
r.update()

r.destroy()
MarianD
  • 13,096
  • 12
  • 42
  • 54
2

On Windows, you can use: pywin32.

Install it with: pip install pywin32.

Use it like this.

import win32clipboard


def to_clipboard(txt):

    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(txt)
    win32clipboard.CloseClipboard()
bravhek
  • 155
  • 5
2

Solution with stdlib, without security issues

The following solution works in Linux without any additional library and without the risk of executing unwanted code in your shell.

import subprocess

def to_clipboard(text: str) -> None:
    sp = subprocess.Popen(["xclip"], stdin=subprocess.PIPE, 
                                      stdout=subprocess.PIPE)
    sp.communicate(text.encode("utf8"))

Note that there multiple clipboard in Linux, the you use with the Middle Mouse (Primary) and yet another that you use pressing STRG+C,STRG+V.

You can define which clipboard is used by adding a selection parameter i.e. ["xclip", "-selection", "clipboard"]. See the man xclip for details.

If you using Windows, just replace xclip with clip.

This solution works without Tkinter, which not available some Python installations (i.e. the custom build I am currently using).

Kound
  • 1,835
  • 1
  • 17
  • 30
  • The `sp.communicate` call does not return, a possible solution is to add a `timeout=t_in_seconds` optional argument, and catch the ensuing `subprocess.TimeoutExpired` exception. – gboffi Oct 07 '22 at 13:04
  • I think thet better and simpler is `subprocess.run(["xclip"], input=text.encode("utf8"))` – MarrekNožka Dec 28 '22 at 11:39
  • In my test env using input didn't work. – Kound Dec 29 '22 at 17:43
2

This works like a charm. It's cross platform with no dependencies. Tested on Linux and Windows.

import PySimpleGUI as sg

text = 'My text to clipboard'

# Set clipboard
sg.clipboard_set(text)

# Paste clipboard
new_text = sg.clipboard_get()
print(new_text)
  • Thank you for the PySimpleGUI mention! It's great to see people have found the [clipboard APIs](https://www.pysimplegui.org/en/latest/call%20reference/#clipboard-apis) and are using them. – Mike from PSG Aug 15 '23 at 12:31
1

In addition to Mark Ransom's answer using ctypes: This does not work for (all?) x64 systems since the handles seem to be truncated to int-size. Explicitly defining args and return values helps to overcomes this problem.

import ctypes
import ctypes.wintypes as w

CF_UNICODETEXT = 13

u32 = ctypes.WinDLL('user32')
k32 = ctypes.WinDLL('kernel32')

OpenClipboard = u32.OpenClipboard
OpenClipboard.argtypes = w.HWND,
OpenClipboard.restype = w.BOOL

GetClipboardData = u32.GetClipboardData
GetClipboardData.argtypes = w.UINT,
GetClipboardData.restype = w.HANDLE

EmptyClipboard = u32.EmptyClipboard
EmptyClipboard.restype = w.BOOL

SetClipboardData = u32.SetClipboardData
SetClipboardData.argtypes = w.UINT, w.HANDLE,
SetClipboardData.restype = w.HANDLE

CloseClipboard = u32.CloseClipboard
CloseClipboard.argtypes = None
CloseClipboard.restype = w.BOOL

GHND = 0x0042

GlobalAlloc = k32.GlobalAlloc
GlobalAlloc.argtypes = w.UINT, w.ctypes.c_size_t,
GlobalAlloc.restype = w.HGLOBAL

GlobalLock = k32.GlobalLock
GlobalLock.argtypes = w.HGLOBAL,
GlobalLock.restype = w.LPVOID

GlobalUnlock = k32.GlobalUnlock
GlobalUnlock.argtypes = w.HGLOBAL,
GlobalUnlock.restype = w.BOOL

GlobalSize = k32.GlobalSize
GlobalSize.argtypes = w.HGLOBAL,
GlobalSize.restype = w.ctypes.c_size_t

unicode_type = type(u'')

def get():
    text = None
    OpenClipboard(None)
    handle = GetClipboardData(CF_UNICODETEXT)
    pcontents = GlobalLock(handle)
    size = GlobalSize(handle)
    if pcontents and size:
        raw_data = ctypes.create_string_buffer(size)
        ctypes.memmove(raw_data, pcontents, size)
        text = raw_data.raw.decode('utf-16le').rstrip(u'\0')
    GlobalUnlock(handle)
    CloseClipboard()
    return text

def put(s):
    if not isinstance(s, unicode_type):
        s = s.decode('mbcs')
    data = s.encode('utf-16le')
    OpenClipboard(None)
    EmptyClipboard()
    handle = GlobalAlloc(GHND, len(data) + 2)
    pcontents = GlobalLock(handle)
    ctypes.memmove(pcontents, data, len(data))
    GlobalUnlock(handle)
    SetClipboardData(CF_UNICODETEXT, handle)
    CloseClipboard()

#Test run
paste = get
copy = put
copy("Hello World!")
print(paste())
M.Bauer
  • 19
  • 3
1

also you can use > clipboard

import clipboard

def copy(txt):
    clipboard.copy(txt)
    
copy("your txt")
bnh
  • 107
  • 1
  • 1
  • 5
1

If (and only if) the application already uses Qt, you can use this (with the advantage of no additional third party dependency)

from PyQt5.QtWidgets import QApplication
clipboard = QApplication.clipboard()

# get text (if there's text inside instead of e.g. file)
clipboard.text()

# set text
clipboard.setText(s)

This requires a Qt application object to be already constructed, so it should not be used unless the application already uses Qt.

Besides, as usual, in X systems (and maybe other systems too), the content only persist until the application exists unless you use something like parcellite or xclipboard.

Documentation:

See also: python - PyQT - copy file to clipboard - Stack Overflow

user202729
  • 3,358
  • 3
  • 25
  • 36
0
import wx

def ctc(text):

    if not wx.TheClipboard.IsOpened():
        wx.TheClipboard.Open()
        data = wx.TextDataObject()
        data.SetText(text)
        wx.TheClipboard.SetData(data)
    wx.TheClipboard.Close()

ctc(text)
jlk
  • 1
  • 1
  • 3
    An explanation of the problem and solution would be helpful. – showdev Apr 14 '15 at 18:16
  • a function to copy text to a clipboard. using the wx python library (I never learnt TK) another solution to the question asked here. – jlk Apr 14 '15 at 20:30
  • Feel free to edit the answer to include your explanation of problem/solution @jlk -- doing this generally makes answers more useful. Also, thanks for trying, but I suppose the original poster had some reason to ask for Tk specifically. – icedwater Apr 15 '15 at 02:14
0

The snippet I share here take advantage of the ability to format text files: what if you want to copy a complex output to the clipboard ? (Say a numpy array in column or a list of something)

import subprocess
import os

def cp2clip(clist):

    #create a temporary file
    fi=open("thisTextfileShouldNotExist.txt","w")

    #write in the text file the way you want your data to be
    for m in clist:
        fi.write(m+"\n")

    #close the file
    fi.close()

    #send "clip < file" to the shell
    cmd="clip < thisTextfileShouldNotExist.txt"
    w = subprocess.check_call(cmd,shell=True)

    #delete the temporary text file
    os.remove("thisTextfileShouldNotExist.txt")

    return w

works only for windows, can be adapted for linux or mac I guess. Maybe a bit complicated...

example:

>>>cp2clip(["ET","phone","home"])
>>>0

Ctrl+V in any text editor :

ET
phone
home
0

Use python's clipboard library!

import clipboard as cp
cp.copy("abc")

Clipboard contains 'abc' now. Happy pasting!

Cribber
  • 2,513
  • 2
  • 21
  • 60
0

You can use winclip32 module! install:

pip install winclip32

to copy:

import winclip32
winclip32.set_clipboard_data(winclip32.UNICODE_STD_TEXT, "some text")

to get:

import winclip32
print(winclip32.get_clipboard_data(winclip32.UNICODE_STD_TEXT))

for more informations: https://pypi.org/project/winclip32/

pypcdev
  • 11
  • 1
0

My multiplatform solution base on this question:

import subprocess
import distutils.spawn

def clipit(text):
    if distutils.spawn.find_executable("xclip"):
        # for Linux
        subprocess.run(["xclip", "-i"], input=text.encode("utf8"))
    elif distutils.spawn.find_executable("xsel"):
        # for Linux
        subprocess.run(["xsel", "--input"], input=text.encode("utf8"))
    elif distutils.spawn.find_executable("clip"):
        # for Windows
        subprocess.run(["clip"], input=text.encode("utf8"))
    else:
        import pyperclip

        print("I use module pyperclip.")
        pyperclip.copy(text)

MarrekNožka
  • 343
  • 2
  • 6
-1

Code snippet to copy the clipboard:

Create a wrapper Python code in a module named (clipboard.py):

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Clipboard
def setText(text):
    Clipboard.SetText(text)

def getText():
    return Clipboard.GetText()

Then import the above module into your code.

import io
import clipboard
code = clipboard.getText()
print code
code = "abcd"
clipboard.setText(code)

I must give credit to the blog post Clipboard Access in IronPython.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nagesh
  • 1
  • 1
-2

you can try this:

command = 'echo content |clip'
subprocess.check_call(command, shell=True)
Y.Max
  • 1
  • 1