3

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

Community
  • 1
  • 1
JinSnow
  • 1,553
  • 4
  • 27
  • 49

1 Answers1

1

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()
JinSnow
  • 1,553
  • 4
  • 27
  • 49