Skip to main content

Full toolkit agnostic cross-platform clipboard access

Project description

klembord

klembord is a python 3 package that provides full clipboard access on supported platforms (Linux and Windows for now, though this may change in the future). klembord has minimal dependencies, depending only on platform specific apis, which means it can be used with any graphics toolkit or without a toolkit at all.


If you find this software useful,

please Donate


Install and dependencies

klembord uses python-xlib under Linux and ctypes on Windows. When installing with pip dependencies will be taken care of automatically.

pip install klembord

That's it!

Usage

>>> import klembord
>>> klembord.init()
>>> klembord.get_text()
'example clipboard text'
>>>klembord.set_text('some string')

klembord also includes convenience functions for working with rich text:

>>> klembord.get_with_rich_text()
('example html', '<i>example html</i>')
>>> klembord.set_with_rich_text('plain text', '<b>plain text</b>')

Rich text function set platform's unicode and html formats.

On Linux accessing selections other than CLIPBOARD is easy, just pass selection name to init:

klembord.init('PRIMARY')

If you need access to other targets/formats you can use get and set functions:

>>> content = {'UTF8_STRING': 'string'.encode(), 'text/html': '<s>string</s>'.encode()}
>>> klembord.set(content)
>>> klembord.get(['UTF8_STRING', 'text/html', 'application/rtf'])
{'UTF8_STRING': b'string', 'text/html': b'<s>string</s>', 'application/rtf': None}

>>> from collections import OrderedDict
>>> content = OrderedDict()
>>> content['HTML Format'] = klembord.wrap_html('<a href="example.com">Example</a>')
>>> content['CF_UNICODETEXT'] = 'Example'.encode('utf-16le')
>>> klembord.set(content)
>>> klembord.get(['HTML Format', 'CF_RTF'])
{'HTML Format': b'<a href="example.com">Example</a>', 'CF_RTF': None}

These examples show manual way of setting clipboard with rich text. Unlike convenience functions get and set takes dicts of bytes as arguments. Key should be target/format string and value binary data or encoded string. Every given format/target will be set.

The first example is Linux usage. Most targets are encoded with utf8 and it's all fairly simple. The second shows usage on Windows. Now windows retrieves formats in order they were defined, so using collections.OrderedDict is a good idea to ensure that say html format takes precedence over plain text. CF_UNICODE, the unicode text format is always encoded in utf-16le. If you set this target with utf8 you'll get unknown characters when pasting. Another thing to note is the wrap_html function. While setting plain html works on Linux, Windows uses it's own (unnecessary) format. This function takes html fragment string and returns formatted bytes object. wrap_html is only available on Windows.

To list available targets/formats:

>>> klembord.get(['TARGETS'])
{'TARGETS': ['TARGETS', 'SAVE_TARGETS', 'UTF8_STRING', 'STRING']}

Clipboard persistence on Linux

As of version 0.1.3 klembord supports storing content in clipboard after application exit. You do need to call klembord.store() explicitly. Note that this method raises AttributeError on Windows.

Selection object

If you need to access PRIMARY selection at the same time as clipboard or you prefer working with objects rather than module level functions, you can use Selection objects.

from klembord import Selection

These objects have the same methods as module level functions, with klembord.init(SELECTION) being the Selection.__init__(SELECTION).

Why klembord

klembord means clipboard in dutch. Since every reasonable name in english was taken on pypi, I decided to cosult a dictionary. Now you might think since there're so many packages for clipboard access klembord is unnecessary. Alas, all the other packages only work with plain text, depend on heavy toolkits or external executables, and in one particular case the entire package simply imports copy and paste functions from pyperclip. I found the situation rather sad, so I decided to write klembord.

Bugs and limitations

  • Setting binary formats should work in theory but it's mostly untested.
  • Setting/getting Windows built in binary formats (e.g. CF_BITMAP) doesn't work and WILL crash python. These require special handling which is currently not implemented in klembord

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

klembord-0.2.2.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

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

klembord-0.2.2-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file klembord-0.2.2.tar.gz.

File metadata

  • Download URL: klembord-0.2.2.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/2.7.17 Linux/5.3.0-64-generic

File hashes

Hashes for klembord-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a64e47640988dfdd1114be29dce3fc88807863c160be32498b5cae65f5626119
MD5 afd38bcc0c78a0d59bb032c745743d9d
BLAKE2b-256 bbf7b76bbb5d6ad78f441458321725788cd78912b248837f080c295e24fc8e1d

See more details on using hashes here.

File details

Details for the file klembord-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: klembord-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/2.7.17 Linux/5.3.0-64-generic

File hashes

Hashes for klembord-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ae087daf661d64615768d5608aa99d71ce16f7cf1089c6ec8ad1e8cc66e4437b
MD5 b2c906084a2347f4e8d7089292dd2fc0
BLAKE2b-256 7316e03c8d359bf7e920abcda4a1b0e0cb652c25e49b2eaa6d6392964c0cfae2

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