Copy and paste from / to clipboard using python / win32

I downloaded win32 for python 2.6 from this site .

This is the code to get / set the clipboard.

def test ():
    Openclipboard () 
    d = GetClipboardData (win32con.CF_TEXT) # get clipboard data
    SetClipboardData (win32con.CF_TEXT, "Hello") # set clipboard data
    CloseClipboard ()

if __name__ == '__main__':
    if sys.platform == 'win32':
        from win32clipboard import *
        import win32gui, win32con
        test ()

It works well with GetClipboarData, but SetClipboardData does not seem to work, because when I run test (), I expect to get "hello" using ^ V, but what I copied before.

What could be wrong?

+3
source share
3 answers

To put data on the clipboard, you want to open the clipboard, then call EmptyClipboardto SetClipboardData.

+5
source

You can also use the pyperclip.py module to avoid win32 dependency. This is just one python module, which is a cross-platform, and for Windows it directly calls DLL calls:

http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/

+2
source

, win32, Tkinter python, : Windows Python?

+1

Source: https://habr.com/ru/post/1767295/


All Articles