[python-uk] Windows Clipboard Module

2016-06-28 Thread John Sampson
I am using win32clipboard module with Python 2.7 in Windows 7. I have 
code which uses GetClipboardData() and SetClipboardText(argument). 
Before running the code I copy text into the Windows clipboard from a 
proprietary program. I know that this program adds garbage to the end of 
the string, namely null-value bytes and possibly other invisible stuff.


When I paste the contents of the Windows clipboard into a Tkinter text 
box I get the string obtained by GetClipboardData() although I have 
since run SetClipboardText(argument).


If I paste the contents of the Windows clipboard at this stage into the 
Boxer text editor I get the string placed by SetClipboardText(argument). 
If I paste into the Tkinter program again, or into Vim or into Microsoft 
Word, I get the string obtained using GetClipboardData(). I can repeat 
this as many times as I like - one string into Boxer, the other string 
into the other programs.


I deduce that both strings must be in the clipboard at the same time, 
but it depends which program has focus which string is pasted in using 
Control v.


Is there a way of showing the contents of the clipboard, garbage and 
all? I would like to find a way of making the Windows clipboard behave 
as it is supposed to - new contents overwriting the old.


Regards

John Sampson

___
python-uk mailing list
python-uk@python.org
https://mail.python.org/mailman/listinfo/python-uk


Re: [python-uk] Windows Clipboard Module

2016-06-28 Thread Pete Forman
John Sampson  writes:

> I am using win32clipboard module with Python 2.7 in Windows 7. I have
> code which uses GetClipboardData() and SetClipboardText(argument).
> Before running the code I copy text into the Windows clipboard from a
> proprietary program. I know that this program adds garbage to the end
> of the string, namely null-value bytes and possibly other invisible
> stuff.
>
> When I paste the contents of the Windows clipboard into a Tkinter text
> box I get the string obtained by GetClipboardData() although I have
> since run SetClipboardText(argument).
>
> If I paste the contents of the Windows clipboard at this stage into
> the Boxer text editor I get the string placed by
> SetClipboardText(argument). If I paste into the Tkinter program again,
> or into Vim or into Microsoft Word, I get the string obtained using
> GetClipboardData(). I can repeat this as many times as I like - one
> string into Boxer, the other string into the other programs.
>
> I deduce that both strings must be in the clipboard at the same time,
> but it depends which program has focus which string is pasted in using
> Control v.
>
> Is there a way of showing the contents of the clipboard, garbage and
> all? I would like to find a way of making the Windows clipboard behave
> as it is supposed to - new contents overwriting the old.

I don't have access to a Windows PC at the moment but here are a couple
of scripts I wrote 9 years ago to play with the clipboard.

Bear in mind that the clipboard is not just text. It is written to in a
number of formats and is designed that the reader pull off the best,
whatever that might be.

There are a number of clipboard viewer or manager applications available
on Windows. Something to watch out for is that clipboard behaviour may
be affected by adding viewers.


=== clipboardViewer.py:
import win32clipboard

def getClipboardViewer():
return win32clipboard.GetClipboardViewer()

if __name__ == '__main__':
print getClipboardViewer()


=== clipboard.py:
import collections
import win32clipboard
import pywintypes

class defaultdict1(collections.defaultdict):  # defaultdict is new in Python 2.5
def __missing__(self, key):
value = self.default_factory(key)
self[key] = value
return value

def getClipboardFormatName(format):
try: # succeeds for registered formats
return win32clipboard.GetClipboardFormatName(format)
except pywintypes.error:
return 'Unregistered format: %d' % format

formatName = defaultdict1(getClipboardFormatName)
for s in dir(win32clipboard):
if s.startswith('CF_'):
formatName[getattr(win32clipboard, s)] = s
formatName[17] = 'CF_DIBV5'  # since Windows 2000, not in pywin32-210

def getAvailableFormats():
"""Return a list of format numbers available on the clipboard."""
formats = []
try:
win32clipboard.OpenClipboard(0)
cf = win32clipboard.EnumClipboardFormats(0)
while (cf != 0):
formats.append(cf)
cf = win32clipboard.EnumClipboardFormats(cf)
finally:
win32clipboard.CloseClipboard()
return formats

if __name__ == '__main__':
for f in sorted(getAvailableFormats()):  # sorted is new in Python 2.4
print '0x%04x %s' % (f, formatName[f])


-- 
Pete Forman
https://payg-petef.rhcloud.com
(formerly on http://petef.22web.org/payg.html)

___
python-uk mailing list
python-uk@python.org
https://mail.python.org/mailman/listinfo/python-uk