Search code examples
pythonwxpythonclipboardobjectlistview

Python win32clipboard data being truncated


I am using a Python module called ObjectListView as an addition to wxPython. I'm using python2.7 and wxPython 2.8.1.2.1

My problem is copying information to the windows clipboard. The module ObjectListView has a section that uses win32clipboard to store information in the clipboard. But, when retrieving the information, only the first character is returned. . .and nothing else.

    try:
        win32clipboard.OpenClipboard(0)
        win32clipboard.EmptyClipboard()
        cfText = 1
        print txt #prints 'hello world'
        win32clipboard.SetClipboardData(cfText, txt)
        print htmlForClipboard #prints html output
        cfHtml = win32clipboard.RegisterClipboardFormat("HTML Format")
        win32clipboard.SetClipboardData(cfHtml, htmlForClipboard)
        print win32clipboard.GetClipboardData() #prints 'h'
    finally:
        win32clipboard.CloseClipboard()

That is the code from the module. I have entered print statements for debugging. I have commented the text that prints. This problem only happens in this module. If I run that section of code in the python interpreter, it functions fine and the clipboard returns the entire input.

What could be causing this problem?


Solution

  • When a string is clipped to the first character, the first thing I think of is that UTF-16 is being interpreted as an 8-bit character. The second byte of a 2-byte UTF-16 sequence for most European languages is zero, and results in an early termination of the string. Try this:

    print win32clipboard.GetClipboardData().decode('utf-16le')
    

    I'd also use an encode('utf-16le') when setting the data to the clipboard.