Python Clipboard Access on Ubuntu | Generated by AI

Home PDF

To read from the clipboard (pasteboard) in Ubuntu using Python, you can use libraries like pyperclip or interact with system clipboard utilities like xclip or xsel. Below are two common approaches:

Approach 1: Using pyperclip

pyperclip is a cross-platform Python library for accessing the clipboard.

  1. Install pyperclip:
    pip install pyperclip
    
  2. Python code to read from clipboard:
    import pyperclip
    
    # Read the contents of the clipboard
    clipboard_content = pyperclip.paste()
    print(clipboard_content)
    
  3. Notes:
    • Ensure xclip or xsel is installed on Ubuntu, as pyperclip relies on them:
      sudo apt update
      sudo apt install xclip xsel
      
    • This method is simple and works across platforms.

Approach 2: Using xclip Directly

If you prefer to interact with the system clipboard directly, you can use xclip.

  1. Install xclip:
    sudo apt update
    sudo apt install xclip
    
  2. Python

System: Python code to read from clipboard using xclip:

   import subprocess

   # Read the contents of the clipboard
   process = subprocess.run(['xclip', '-selection', 'clipboard', '-o'], capture_output=True, text=True)
   clipboard_content = process.stdout
   print(clipboard_content)
  1. Notes:
    • This approach directly calls the xclip command-line tool.
    • Use -selection clipboard for the modern clipboard (Ctrl+C/Ctrl+V) or -selection primary for the mouse-selection clipboard.
    • Ensure xclip is installed.

Recommendations

Tested on Ubuntu 22.04 with Python 3.10. If you encounter issues, ensure the required tools (xclip or xsel) are installed and the clipboard contains text data.


Back 2025.06.23 Donate