How do I paste the copied text from the keyboard in Python?

Python's pyperclip module provides a simple way to access clipboard content and paste copied text in your applications. This cross-platform library works on Windows, macOS, and Linux systems.

Installation

First, install pyperclip using pip ?

pip install pyperclip

Basic Clipboard Operations

The pyperclip module provides two main functions for clipboard operations ?

import pyperclip

# Copy text to clipboard
pyperclip.copy("Hello, World!")

# Paste text from clipboard
clipboard_text = pyperclip.paste()
print(clipboard_text)
Hello, World!

Creating a GUI Application with Tkinter

Here's a practical example that creates a text editor with paste functionality ?

# Import required libraries
from tkinter import *
import pyperclip

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")
win.title("Clipboard Text Paster")

# Create a text widget
text_widget = Text(win, height=15, width=80)
text_widget.pack(pady=20)

def paste_clipboard_text():
    """Function to paste clipboard content into text widget"""
    clipboard_content = pyperclip.paste()
    text_widget.insert(END, clipboard_content)

def clear_text():
    """Function to clear the text widget"""
    text_widget.delete(1.0, END)

# Create buttons for paste and clear operations
paste_button = Button(win, text="Paste from Clipboard", command=paste_clipboard_text, 
                     bg="lightblue", padx=10, pady=5)
paste_button.pack(pady=5)

clear_button = Button(win, text="Clear Text", command=clear_text, 
                     bg="lightcoral", padx=10, pady=5)
clear_button.pack(pady=5)

# Start the GUI event loop
win.mainloop()

How It Works

The application works as follows ?

  • pyperclip.paste() retrieves the current clipboard content
  • text_widget.insert(END, content) adds the pasted text to the end of the text widget
  • The GUI provides buttons to paste clipboard content and clear the text area

Key Features

Function Description Usage
pyperclip.copy() Copy text to clipboard pyperclip.copy("text")
pyperclip.paste() Get text from clipboard text = pyperclip.paste()

Conclusion

The pyperclip module makes clipboard operations simple in Python applications. Use pyperclip.paste() to retrieve copied text and integrate it into GUI applications for enhanced user experience.

Updated on: 2026-03-26T18:52:13+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements