简体   繁体   中英

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?

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.

Instead of using ObjectListView for copying and pasting, you should try the methods included with wxPython. Here are some relevant links:

I've never had any problem using that for simple copy and paste actions.

I found a similar post here: wxPython ObjectListView Capture Ctrl-C shortcut

I was having the same problem and decided to override the ObjectListView class. I only wanted the text copied anyway, and I used Mike D's example code to copy it to the clipboard.

I also took the opportunity to copy the column text as a header to my data. This made the info pasted into Excel much easier to parse.

Here is my code:

class MyOLVClass(ObjectListView):
    def CopyObjectsToClipboard(self, objects):
        """
        Put a textual representation of the given objects onto the clipboard.

        This will be one line per object and tab-separated values per line.
        """
        if objects is None or len(objects) == 0:
            return

        # Get the column header text
        h = []
        for n in range(self.GetColumnCount()):
            col = self.GetColumn(n)
            h.append(col.GetText())
        header = '\t'.join(h)

        # Get all the values of the given rows into multi-list
        rows = self._GetValuesAsMultiList(objects)

        # Make a text version of the values
        lines = [ "\t".join(x) for x in rows ]
        txt = header + '\n' + "\n".join(lines) + "\n"

        self.dataObj = wx.TextDataObject()
        self.dataObj.SetText(txt)
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(self.dataObj)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox("Unable to open the clipboard", "Error")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM