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()

Detach mode

Both copy and copy_image functions support an optional keyword-only bool parameter detach (defaults to False). This feature is only relevant on Linux; using it on Windows or macOS is a no-op.

import copykitten
from PIL import Image

# Copy text with detach.
copykitten.copy("meow", detach=True)

# Copy image with detach.
image = Image.open("image.png")
image_bytes = image.tobytes()
copykitten.copy_image(image_bytes, image.width, image.height, detach=True)

Copying with detach will spawn a background process (a daemon) that will manage the copied content instead of the parent process. This is useful for one-off scripts that copy something to the clipboard and exit shortly after that. In this case, the copied content may become unavailable; there will be nothing to paste. A lot of times, this is not a problem due to Linux distros including a pre-installed clipboard manager that will handle such cases. Still, some distros don't have a clipboard manager.

The background process will run until the clipboard content is overwritten by another copy operation.

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 take 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-2.0.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

copykitten-2.0.0-cp38-abi3-win_amd64.whl (267.3 kB view details)

Uploaded CPython 3.8+Windows x86-64

copykitten-2.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (503.0 kB view details)

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

copykitten-2.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (453.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

copykitten-2.0.0-cp38-abi3-macosx_11_0_arm64.whl (359.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

copykitten-2.0.0-cp38-abi3-macosx_10_12_x86_64.whl (381.1 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file copykitten-2.0.0.tar.gz.

File metadata

  • Download URL: copykitten-2.0.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for copykitten-2.0.0.tar.gz
Algorithm Hash digest
SHA256 bda13d614ffb38147d4fce5217d4f2397967514f5ea09fb8d5c5cf5d16028e58
MD5 235a53775bd846ad993c0445665c0cc6
BLAKE2b-256 c17257a5dee794b29c940f6bc429ff731c3d0b9f7af0aad94bd88bafbc3db07f

See more details on using hashes here.

File details

Details for the file copykitten-2.0.0-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for copykitten-2.0.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 00d09e79fe8031dbdfaefd843d60e48f2b63e6ea9a823d25e07a17469f25017e
MD5 f39094bdde1b02559c5f1fd8b8a272db
BLAKE2b-256 604522e1d8ffecadcfb0f57ba5189b6c2a7850621ad2045cbdf23a4d7f1f1849

See more details on using hashes here.

File details

Details for the file copykitten-2.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for copykitten-2.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65b799e46da487e3380248065e5684d39e9f8f0f53b1124edba5e4fd2dc5a273
MD5 e65e854ed532c7cce63eb1e0a49414f3
BLAKE2b-256 98595800f845f87f5841f467082698ccbe40e7eb000b1e069bdf24134c91c14c

See more details on using hashes here.

File details

Details for the file copykitten-2.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for copykitten-2.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6be53c8c45bb85bda1679ea2d890e24303096d2a9d9a1022e159b7cfd350b2c0
MD5 3d6a4b87514ef0f3f93e27e4c5f9acbf
BLAKE2b-256 3e3cba4ce92ffe21c695d1492fe495032eb2356a5ade4a497d238f7efd277cea

See more details on using hashes here.

File details

Details for the file copykitten-2.0.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for copykitten-2.0.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1fb3118f5b3d9bc70442ae62f5d0b5fdd22243f2b4f2811e0680cbb4dfca112a
MD5 8d2f75be08b720536ffb15fcb0968e7d
BLAKE2b-256 b5aa51d0d24435846eb8c41a5f68874a2a39d367e22c58eb3a955455b1e0c7bf

See more details on using hashes here.

File details

Details for the file copykitten-2.0.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for copykitten-2.0.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 191c202d19962fa5dbed5296d83bfa844b36609e30f0fb19cf1706b136f170e8
MD5 f245febdd94503236b2c8e0e7d3993c8
BLAKE2b-256 3f242ff5084b1fe4825d47246de8f81fa738245ca6d6645b31dfc686bcbed51e

See more details on using hashes here.

Supported by

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