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

后端 未结 23 2846
温柔的废话
温柔的废话 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:40

    Code snippet to copy the clipboard:

    Create a wrapper Python code in a module named (clipboard.py):

    import clr
    clr.AddReference('System.Windows.Forms')
    from System.Windows.Forms import Clipboard
    def setText(text):
        Clipboard.SetText(text)
    
    def getText():
        return Clipboard.GetText()
    

    Then import the above module into your code.

    import io
    import clipboard
    code = clipboard.getText()
    print code
    code = "abcd"
    clipboard.setText(code)
    

    I must give credit to the blog post Clipboard Access in IronPython.

提交回复
热议问题