Skip to main content
  1. Posts/

How to Get or Set Clipboard Text in Python

·107 words·1 min·
Python
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

Run the Job Immediately after Starting Scheduler in Python APScheduler
·322 words·2 mins
Python APScheduler
Scheduling Your Tasks with Apscheduler
··367 words·2 mins
Python APScheduler
Post Nested Data Structure to the Server Using Requests
··394 words·2 mins
Python Requests HTTP Flask