[PyKDE] Help with simple clipboard examples
Stephen Boulet
stephen at theboulets.net
Wed Oct 27 20:42:55 BST 2004
On Wed, 27 Oct 2004 18:47:31 +0200, Sundance wrote
> I heard Stephen Boulet said:
>
> > I'd like to get and set the X-window clipboard for some short
> > scripts, but I'm not doing it quite right. Can someone help me along?
>
> Okay, it seems to work here (I just did the imports outside the
> function calls, since doing it inside functions is -bad- practice).
> What is your problem exactly?
I just want to have a module to use under linux that would let me get and set
the clipboard (I usually get the clipboard, process it in some way, and then
paste back to it; one example might be to format the text on the clipboard for
restructured text). A python interface to xwindows might work too.
I was trying to use the 'xsel' program to accomplish the same thing, but xsel
mangles non-ascii characters at the moment:
clipboard.py:
"""
Contains functions to read and write to the clipboard.
In non-windows systems uses the PRIMARY clipboard via
the 'xsel' command.
'xsel' is available here: http://www.niksula.cs.hut.fi/~vherva/xsel/
"""
from sys import platform
if platform == 'win32':
....from win32clipboard import *
....from win32con import CF_TEXT
....
....def getclipboard():
........OpenClipboard()
........s = GetClipboardData(CF_TEXT)
........CloseClipboard()
........return s
....
....def setclipboard(s):
........OpenClipboard()
........EmptyClipboard()
........SetClipboardText(s)
........CloseClipboard()
else:
....from os import popen
....def getclipboard():
........s = popen('xsel').read()
........return s
....
....def setclipboard(s):
........if s[0] != '"':
............command = 'echo "%s" | xsel -s -i' % s
........else:
............command = "echo '%s' | xsel -s -i" % s
........popen(command).read()
if __name__ == '__main__':
....s = 'André'
....setclipboard(s)
_________
Stephen
If your desktop gets out of control easily,
you probably have too much stuff on it that
doesn't need to be there.
Donna Smallin, "Unclutter Your Home"
More information about the PyQt
mailing list