Skip to main content
  1. Posts/

How to Get or Set Clipboard Text in Python

·107 words·1 min·
Table of 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()

Ref
#

Related

Scheduling Your Tasks with Package Apscheduler
··367 words·2 mins
Post Nested Data Structure to the Server Using Requests
··394 words·2 mins
Configure Python logging with dictConfig
··503 words·3 mins