Python Clipboard Manager
This utility monitors the system clipboard for changes and logs every copied item to a history file. It uses the pyperclip module, which provides a cross-platform clipboard text API.
Modules Used:
pyperclip: To access the system clipboard (copy/paste).time: To handle polling intervals and timestamps.
Installation
pip install pyperclip
Note: On Linux, you may need to install xclip or xsel via your package manager (e.g., sudo apt install xclip).
The Code
Save this as clipboard_monitor.py.
import pyperclip
import time
import sys
def monitor_clipboard(history_file="clipboard_history.txt"):
print(f"Monitoring clipboard... History saved to '{history_file}'")
print("Press Ctrl+C to stop.")
# Initialize with current clipboard content to avoid duplicate first entry
last_value = pyperclip.paste()
try:
while True:
# Get current clipboard content
current_value = pyperclip.paste()
# Check if it has changed
if current_value != last_value:
last_value = current_value
# Print to console (truncated)
preview = current_value.replace('\n', ' ')
if len(preview) > 50:
preview = preview[:47] + "..."
print(f"Captured: {preview}")
# Save to file with timestamp
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
with open(history_file, "a", encoding="utf-8") as f:
f.write(f"[{timestamp}]\n{current_value}\n{'-'*40}\n")
# Check every 0.5 seconds
time.sleep(0.5)
except KeyboardInterrupt:
print("\nClipboard monitor stopped.")
if __name__ == "__main__":
monitor_clipboard()
Usage
python clipboard_monitor.py
- Run the script.
- Copy text from any application (Browser, Notepad, etc.).
- The script will detect the change, print a preview to the console, and append the full text to
clipboard_history.txt.