Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 3.6 windows: retrieving the clipboard CF_HTML format

I want to copy some rich text, modify its source code (changing some tags and text, using regex and/or beautifulsoup) and send it back to the clipboard. I'm looking for the easiest way to do that.

I tried win32clipboard, but it doesn't support the CF_HTML format (windows clipboard contains many formats).

So I'm looking for a module that could help me to get this format: if the CF_HTML clipboard format contains HTML, store it in that variable, do some operation, then send it back. (Optionally: and do other stuff on other clipboard formats)

Here is a Linux equivalent of what I'm looking for. It retrieves the HTML source, when there's some in the clipboard (source)

#!/usr/bin/env python
import gtk
print (gtk.Clipboard().wait_for_contents('text/html')).data

Edit1: There is a work around with pywin32 using this script. But is there a module able to do that directly (if CF_HTML contains data, get it, and send it back)?

like image 819
JinSnow Avatar asked May 13 '26 08:05

JinSnow


1 Answers

The Edit1 solution seems to be actually the best.

  1. put the script above (HtmlClipboard.py) in the python module folder: C:\Users\xxx\AppData\Local\Programs\Python\Python36\Lib\site-packages
  2. install win32clipboard
  3. With the 2 points above you could play with a script like this:

    #get CF_Html Clipboard
    import HtmlClipboard #.py script found in github
    if HtmlClipboard.HasHtml():
        # print('there is HTML!!')
        dirty_HTML = HtmlClipboard.GetHtml()
        print(dirty_HTML)
    else:
        print('no html')
    
    dirty_HTML= clean_HTML #do what you want with it
    
    #put data to clipboard:
    HtmlClipboard.PutHtml(clean_HTML)
    

Bonus:

##get CF_TEXT from clipboard
import win32clipboard
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
win32clipboard.CloseClipboard()
like image 104
JinSnow Avatar answered May 14 '26 22:05

JinSnow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!