How do I copy a string to the clipboard on Windows using Python?

后端 未结 23 3281
温柔的废话
温柔的废话 2020-11-22 03:13

I\'m trying to make a basic Windows application that builds a string out of user input and then adds it to the clipboard. How do I copy a string to the clipboard using Pytho

23条回答
  •  我在风中等你
    2020-11-22 03:46

    I've tried various solutions, but this is the simplest one that passes my test:

    #coding=utf-8
    
    import win32clipboard  # http://sourceforge.net/projects/pywin32/
    
    def copy(text):
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
        win32clipboard.CloseClipboard()
    def paste():
        win32clipboard.OpenClipboard()
        data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
        win32clipboard.CloseClipboard()
        return data
    
    if __name__ == "__main__":  
        text = "Testing\nthe “clip—board”: 

提交回复
热议问题