0% found this document useful (0 votes)
40 views10 pages

Pyperclip Clipboard Python

python notes

Uploaded by

Kavitha Palani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Pyperclip Clipboard Python

python notes

Uploaded by

Kavitha Palani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Working with Clipboard in

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!'

• • paste() returns whatever text is currently in the


clipboard.
🔁 If You Change the Clipboard Manually
• If you manually copy new content from
outside the program,
• pyperclip.paste() will return that new content.

• 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

You might also like