PyObjC is the way to go:
#!/usr/bin/python
from AppKit import NSPasteboard, NSStringPboardType
pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print u"Pastboard string: %s".encode("utf-8") % repr(pbstring)
This only supports text and will return None otherwise. You can extend it to support other data types as well, see NSPastboard Class Reference.
PyObjC is the way to go:
#!/usr/bin/python
from AppKit import NSPasteboard, NSStringPboardType
pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print u"Pastboard string: %s".encode("utf-8") % repr(pbstring)
This only supports text and will return None otherwise. You can extend it to support other data types as well, see NSPastboard Class Reference.
Have you looked at the xerox module?
It is supposed to support windows, OS X and Linux
Usage is as follows:
xerox.copy(u'some string')
And to paste:
>>> xerox.paste()
u'some string'
» pip install clipboard
How to write a Unicode string to the Mac clipboard:
import subprocess
def write_to_clipboard(output):
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
How to read a Unicode string from the Mac clipboard:
import subprocess
def read_from_clipboard():
return subprocess.check_output(
'pbpaste', env={'LANG': 'en_US.UTF-8'}).decode('utf-8')
Works on both Python 2.7 and Python 3.4.
2021 Update: If you need to be able to read the clipboard on other operating systems and not just Mac and are okay with adding an external library, pyperclip also seems to work well. I tested it on Mac with Unicode text:
python -m pip install pyperclip
python -c 'import pyperclip; pyperclip.copy("私はDavid!")' # copy
python -c 'import pyperclip; print(repr(pyperclip.paste()))' # paste
A simple way:
cmd = 'echo %s | tr -d "\n" | pbcopy' % str
os.system(cmd)
A cross-platform way:
https://stackoverflow.com/a/4203897/805627
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()
» pip install pyperclip
This little piece of ugliness works and loads "image.jpg" onto the clipboard...
#!/usr/bin/env python3
import subprocess
subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file "image.jpg") as JPEG picture)'])
found this solution from https://github.com/RhetTbull/textinator/blob/main/src/pasteboard.py, copying from a .png or .tiff file, similar to the osascript solution but this hooks directly into pyObjC.
from Foundation import NSData
from AppKit import NSPasteboardTypePNG, NSPasteboardTypeTIFF, NSPasteboard
format = "PNG" # (or "TIFF")
filename = "/path/to/image.png" # set this to filepath where img is saved
pasteboard = NSPasteboard.generalPasteboard()
image_data = NSData.dataWithContentsOfFile_(filename)
if format not in ("PNG", "TIFF"):
raise TypeError("Invalid format, must be PNG or TIFF")
format_type = NSPasteboardTypePNG if format == "PNG" else NSPasteboardTypeTIFF
pasteboard.clearContents()
pasteboard.setData_forType_(image_data, format_type)
You can use the module called win32clipboard, which is part of pywin32.
Here is an example that first sets the clipboard data then gets it:
import win32clipboard
# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()
# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print data
An important reminder from the documentation:
When the window has finished examining or changing the clipboard, close the clipboard by calling CloseClipboard. This enables other windows to access the clipboard. Do not place an object on the clipboard after calling CloseClipboard.
you can easily get this done through the built-in module Tkinter which is basically a GUI library. This code creates a blank widget to get the clipboard content from OS.
from tkinter import Tk # Python 3
#from Tkinter import Tk # for Python 2.x
Tk().clipboard_get()
» pip install pyperclip3