How to Get or Set Clipboard Text in Python
Contents
There are several Python packages by which we can get and set the system clipboard.
Tkinter
Use tkinter to get clipboard text.
import tkinter as tk
root = tk.TK()
root.withdraw() # keep the window from showing
print(root.clipboard_get())
pywin32
Use pywin32. It provides the win32clipboard
module.
import win32clipboard
# set the clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('just for a test')
win32clipboard.CloseClipboard()
# get the clipboard text
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
print(data)
win32clipboard.CloseClipboard()
pyperclip
Use pyperclip.
import pyperclip
# set the clipboard
pyperclip.copy('some text')
# get the clipboard
pyperclip.paste()