Skip to main content

A robust, dependency-free way to use the system clipboard in Python.

Project description

copykitten

Code style: black PyPI - Version PyPI - Python Version PyPI - Downloads

A robust, dependency-free way to use the system clipboard in Python.

Installation

copykitten supports Python >= 3.8.

You can install copykitten from PyPI using pip or any other Python package manager.

pip install copykitten

Usage

Text

To copy or paste text content, use copykitten.copy and copykitten.paste functions.

import copykitten

copykitten.copy("The kitten says meow")
import copykitten

text = copykitten.paste()

Image

To copy or paste images, use copykitten.copy_image and copykitten.paste_image functions. Working with images is a bit complex, so read further.

import copykitten
from PIL import Image

image = Image.open("image.png")
pixels = image.tobytes()

copykitten.copy_image(pixels, image.width, image.height)
import copykitten
from PIL import Image

pixels, width, height = copykitten.paste_image()
image = Image.frombytes(mode="RGBA", size=(width, height), data=pixels)
image.save("image.png")

To copy an image to the clipboard, you have to pass three arguments - pixel data, width, and height. Pixel data must be a bytes object containing the raw RGBA value for each pixel. You can easily get it using an imaging library like Pillow.

If your image is not of RGBA type (like a typical JPEG, which is RGB), you first have to convert it to RGBA, otherwise copy_image will raise an exception.

When pasting an image from the clipboard you will receive a 3-tuple of (pixels, width, height). Pixels here are the same RGBA bytes object. Please note that it is not guaranteed that any image copied to the clipboard by another program will be successfully pasted with copykitten.

You can read more about the data format and the implications of working with images in the arboard documentation.

Clear

To clear the clipboard, use copykitten.clear function.

import copykitten

copykitten.clear()

Rationale

At the time of writing, there are very few Python packages that handle the clipboard. Most of them are simply no longer maintained (including the most popular solution around the web, pyperclip).

They all depend on external command-line tools like xclip/pbcopy or libraries like PyQt/GTK. You have to make sure these dependencies are installed on the target machine, otherwise they won’t work.

There are some solutions using the Tkinter library, which comes with the standard Python suite. However, these solutions are fragile and may leave your app unresponsive.

Copykitten is a lightweight wrapper around the Rust arboard library. It comes with pre-built wheels for Linux (x64, ARM64), macOS (x64, ARM64), and Windows (x64), so you don't have to worry about anything.

What's in a name?

You can’t even imagine, how many Python packages devoted to the clipboard management there are on PyPI! Most of them are abandoned for a decade, and all the neat obvious names (and even some rather creative ones) are already taken. So I had no choice, but to invent this tongue-in-cheek name. Also, my wife approved it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

copykitten-1.2.2.tar.gz (17.0 kB view hashes)

Uploaded Source

Built Distributions

copykitten-1.2.2-cp38-abi3-win_amd64.whl (192.6 kB view hashes)

Uploaded CPython 3.8+ Windows x86-64

copykitten-1.2.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (432.5 kB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ x86-64

copykitten-1.2.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (410.4 kB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

copykitten-1.2.2-cp38-abi3-macosx_11_0_arm64.whl (369.4 kB view hashes)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

copykitten-1.2.2-cp38-abi3-macosx_10_12_x86_64.whl (381.1 kB view hashes)

Uploaded CPython 3.8+ macOS 10.12+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page