Pyperclip Clipboard Python
Pyperclip Clipboard Python
Python
Using the pyperclip Module
📋 What is the Clipboard?
• The clipboard is a temporary storage area for
data you copy (Ctrl+C) or cut (Ctrl+X).
• You can paste the clipboard contents using
Ctrl+V.
• Python can interact with the clipboard using
the pyperclip module.
🧰 What Does pyperclip Do?
• The pyperclip module allows your Python
program to copy text to and paste text from
the clipboard.
• Functions: pyperclip.copy() and
pyperclip.paste().
✅ Code Explanation
• import pyperclip
• pyperclip.copy('Hello world!')
• # Copies text to clipboard
• pyperclip.paste()
• # Pastes text from clipboard
The join() Method
• • Combines a list of strings into a single string.
• • Syntax: 'separator'.join(list)
• Examples:
• >>> ', '.join(['cats', 'rats', 'bats'])
• 'cats, rats, bats'
• >>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
• 'MyABCnameABCisABCSimon'
The split() Method
• • Splits a string into a list of strings.
• • Syntax: string.split(delimiter)
• Examples:
• >>> 'My name is Simon'.split()
• ['My', 'name', 'is', 'Simon']
• >>> 'MyABCnameABCisABCSimon'.split('ABC')
• ['My', 'name', 'is', 'Simon']
Removing Whitespace
• • strip(): removes leading and trailing whitespace.
• • lstrip(): removes leading whitespace only.
• • rstrip(): removes trailing whitespace only.
• Example:
• >>> spam = ' Hello World '
• >>> spam.strip() → 'Hello World'
• >>> spam.lstrip() → 'Hello World '
• >>> spam.rstrip() → ' Hello World'
Using pyperclip Module
• • Lets Python access the system clipboard.
• • Functions: copy(), paste()
• Example:
• >>> import pyperclip
• >>> pyperclip.copy('Hello world!')
• >>> pyperclip.paste() → 'Hello world!'
• Example:
• # After copying a sentence manually:
• pyperclip.paste() ➞ returns the new copied
sentence.
📌 Use Cases & Installation
• Use Cases:
• - Automatically copy results for sharing
• - Building clipboard-based tools
• - Automating repetitive copy-paste tasks
• Installation:
• pip install pyperclip