Skip to main content

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

File paths

To copy or paste file paths, use copykitten.copy_paths and copykitten.paste_paths functions.

import copykitten
import pathlib

copykitten.copy_paths('/home/user/.bashrc') # Copy one path.
copykitten.copy_paths('/home/user/.bashrc', '/home/user/.bash_aliases') # Or multiple.
copykitten.copy_paths(pathlib.Path('C://Users/user/file.txt'))  # Or a pathlib.Path.
import copykitten

paths = copykitten.paste_paths()

Note that the files you are about to copy into the clipboard must exist, otherwise an exception will be raised.

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.

Knows issues

For now, there are some limitations that arise from the way the daemon is spawned (namely, as a fork of the parent process).

  1. Interleaving copying w/o detach and with detach (during the execution of a single program) won't work well and may lead to a paste attempt freezing the application until the clipboard is overwritten. Choose one way and stick to it.
  2. If your program utilizes large amounts of memory, this memory won't be freed until the background process exit.

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.

Metadata

Release files for copykitten 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for copykitten 2.1.0
File Size Uploaded
copykitten-2.1.0.tar.gz 21.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for copykitten 2.1.0
File
copykitten-2.1.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
copykitten-2.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details
copykitten-2.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details
copykitten-2.1.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
copykitten-2.1.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.1 MB

Release files / copykitten-2.1.0.tar.gz

Download URL copykitten-2.1.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7cb4c8f86f175d8ea91cac283a4c2ca507b5631d799209e2d1ba4fa35f9e9578
BLAKE2b-256 checksum
How to use checksums
f63042e13b353e3f989da8b9e2a8232cc03e77e00897282b8b6cbfa171c6a2f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / copykitten-2.1.0-cp38-abi3-win_amd64.whl

Download URL copykitten-2.1.0-cp38-abi3-win_amd64.whl
Size 290.2 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
05563cfdb7bbc95c57cd77a859cb81fa737562022862699ee26fff25acfa78df
BLAKE2b-256 checksum
How to use checksums
80ea00378998fa1254054a2a8efc7055cf981c5086fe78b9882a9d94018bdfd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / copykitten-2.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL copykitten-2.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 524.0 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
e1030c84a72dd4453b4c793e3f5ad86983e06754f4b1549ac65c5e006373200d
BLAKE2b-256 checksum
How to use checksums
98cf8119fc85b0a03909eb0b265deeb2351893ef1ef7326e7ee55b1e2d6d1242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / copykitten-2.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL copykitten-2.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 485.7 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
a81fe3239354c52338af05d58ff2651e24efa1bcc14ed92e683a465a45c7ced1
BLAKE2b-256 checksum
How to use checksums
e4b8bbe24c0f73dd8727afee1497b2e04a1bd5aeda56315153a85ae6032f19ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / copykitten-2.1.0-cp38-abi3-macosx_11_0_arm64.whl

Download URL copykitten-2.1.0-cp38-abi3-macosx_11_0_arm64.whl
Size 388.3 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a5c6aeb63274c3a60eed978d72c9e6ba2ff9099b92f86a964edf81f44abfb877
BLAKE2b-256 checksum
How to use checksums
8a8e355f7d0d70d71da0c2ae0176939916cff889808b39a34aea530e71950e6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / copykitten-2.1.0-cp38-abi3-macosx_10_12_x86_64.whl

Download URL copykitten-2.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Size 401.0 kB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
e13809dc131c388944058f5e6093bcd9d01ed50d804e2b93449c065dc82c656a
BLAKE2b-256 checksum
How to use checksums
3573fa2aa87b962b75d65924c5e0392d897aefa446597c3b6dc70158177a681f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release history Release notifications | RSS feed

This release

2.1.0 This release

6 release files

2.0.0

6 release files

1.2.2

6 release files

1.2.1

6 release files

1.2.0

6 release files

1.1.1

6 release files

1.1.0

6 release files

1.0.1

6 release files

1.0.0

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page