【问题标题】:Python win32clipboard data being truncatedPython win32clipboard 数据被截断
【发布时间】:2012-12-18 15:26:37
【问题描述】:

我正在使用一个名为 ObjectListView 的 Python 模块作为 wxPython 的补充。我正在使用 python2.7 和 wxPython 2.8.1.2.1

我的问题是将信息复制到 Windows 剪贴板。模块 ObjectListView 有一个部分使用 win32clipboard 将信息存储在剪贴板中。但是,在检索信息时,只返回第一个字符。 . .没有别的了。

    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()

这是模块中的代码。我已经输入了打印语句进行调试。我已经评论了打印的文本。这个问题只发生在这个模块中。如果我在 python 解释器中运行那段代码,它会正常运行,并且剪贴板会返回整个输入。

什么可能导致这个问题?

【问题讨论】:

    标签: python wxpython clipboard objectlistview


    【解决方案1】:

    当一个字符串被剪辑到第一个字符时,我首先想到的是 UTF-16 被解释为一个 8 位字符。大多数欧洲语言的 2 字节 UTF-16 序列的第二个字节为零,导致字符串提前终止。试试这个:

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

    在将数据设置到剪贴板时,我也会使用encode('utf-16le')

    【讨论】:

    • 太棒了!谢谢,编码是问题。
    【解决方案2】:

    您应该尝试使用 wxPython 中包含的方法,而不是使用 ObjectListView 进行复制和粘贴。以下是一些相关链接:

    我在使用它进行简单的复制和粘贴操作时从未遇到任何问题。

    【讨论】:

      【解决方案3】:

      我在这里找到了一个类似的帖子:wxPython ObjectListView Capture Ctrl-C shortcut

      我遇到了同样的问题并决定重写 ObjectListView 类。无论如何我只想复制文本,我使用 Mike D 的示例代码将其复制到剪贴板。

      我还借此机会将列文本作为标题复制到我的数据中。这使得粘贴到 Excel 中的信息更易于解析。

      这是我的代码:

      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")
      

      【讨论】:

        猜你喜欢
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        相关资源
        最近更新 更多