Sitak Sitak - 13 days ago 5
Python Question

Modifying a clipboard content to be treated as HTML

When I “copy an image” on the web (by highlighting the image and ctrl+C) and then passed it into the text view of the HTML WYSIWYG editor (not the source code editor) the picture is displayed. Even though I paste in the text editor ( source code editor), the content of the clipboard is understood by the editor as an html code.

For example, if I simply paste

“<img src="someURL" />
in the text editor, it will be added in the source code as
“<p>&lt;img src="someURL" /&gt;</p>”
so this clipboard isn’t understood by the editor as an html code.

So how should I modify the content of my clipboard so that an HTML WYSIWYG editor understand it as an html code even though I am pasting it in the text view (not source code editor)?




What I want to do in more details:

when I have the URL of an image stored in my clipboard, I want to be able to add the image to the HTML WYSIWYG editor without having to switch to the source code editor. So I would like to transform the content of my clipboard (by adding some code before and after the URL) so it is understood as HTML code (just like the example mentioned above) by my HTML WYSIWYG editor.




Edit: to better target the answer here is what I try to achieve. When I use shareX to upload a picture, ShareX store automatically this (private) shareable URL in the clipboard. https://drive.google.com/open?id=XXXX
This code convert it in an embedded format. But I'm still looking for a way to store that as html format.

#URL_to_Picture.py
#
#(xxx=FileID)
#
#You need that kind of URL to be able to embed the picture in an editor: https://drive.google.com/uc?export=view&id=XXXX
#
#This script does a part of the job by converting Google drive URL into an embedded (and permanent) link:



from jaraco import clipboard
UrlShareX = clipboard.paste_text()
UrlShareX=UrlShareX.replace("https://drive.google.com/file/d/", "")
UrlShareX=UrlShareX.replace("/view?usp=drivesdk", "")
UrlShareX=UrlShareX.replace("/view?usp=sharing", "")
UrlShareX=UrlShareX.replace("https://drive.google.com/open?id=", "")
URL= '<img src="https://drive.google.com/uc?export=view&id={}" />'.format(UrlShareX)
clipboard.copy_html(URL)


To try on ShareX:


  1. You must set the access to Google drive in ShareX menu:
    destination/destination settings/google drive.

  2. You must set the ShareX menu: “after upload task” to “copy URL to
    clipboard”


Answer

You can do this (warning: for some reasons, this script only works if your url starts with http://)

  1. Install HtmlClipboard : copy the script, save it as HtmlClipboard.py in C:\Python##\Lib\site-packages\
  2. Save the script below as image_link_as_html.py(I used some of your code in your question):
  3. Create a shorcut for the script in step to (right click on the file image_link_as_html.py, and select create a shorcut)
  4. Right click on the shorcut, select Properties, and and add a keyboard shorcut in Shorcut key.

That's it. When you have an image url in our clipboard, you can just press your keyboard shorcut and you can paste your image directly in the html mode of you editor.


image_link_as_html.py (Python34):

from tkinter import Tk
root = Tk()
root.withdraw()
image_url = root.clipboard_get()

# send <img src="https://image_url" alt="" />  to an "HTML format clipboard"
import HtmlClipboard
HtmlClipboard.PutHtml("<img src=\"http://"+image_url+" \" alt=\"\"/>")

To address the part about ShareX, you could use this scrip instead:

from tkinter import Tk
root = Tk()
root.withdraw()
UrlShareX = root.clipboard_get()

# remove everything except the file google ID: this part is not needed 
UrlShareX=UrlShareX.replace("https://drive.google.com/file/d/", "") 
UrlShareX=UrlShareX.replace("/view?usp=drivesdk", "")
UrlShareX=UrlShareX.replace("/view?usp=sharing", "")
UrlShareX=UrlShareX.replace("https://drive.google.com/open?id=", "")
UrlShareX=UrlShareX.replace("/view", "")

# send <img src="https://drive.google.com/uc?export=view&amp;id=xxx " alt="" />  to an "HTML format clipboard"
import HtmlClipboard
HtmlClipboard.PutHtml("<img src=\"https://drive.google.com/uc?export=view&id="+UrlShareX+" \" alt=\"\"/>")