klembord icon indicating copy to clipboard operation
klembord copied to clipboard

Issue with Html and other targets/formats

Open MagTun opened this issue 3 years ago • 4 comments

I am using this old HTMLClibpboard script but I wanted to change for your package but I didn't succeed in making it work.

When I use this

import klembord 
klembord.set_with_rich_text('plain text', '<b><a href="www.google.com">plain text</a></b>')

and then paste in some HTML editor (like the one from zotero which use TinyMCE) then it's the plain text that is sent and not HTML. (I tried on Zotero on two different computers.)

I also tried this

content = {'UTF8_STRING': 'some text'.encode(), 'text/html': '<s>string</s>'.encode()}
klembord.set(content)

but then the clipboard is empty: nothing happens when I paste and

Windows 10, python 3.6

MagTun avatar Apr 13 '21 14:04 MagTun

I had a similar issue. It turns out the size calculations around line 220 in winclipboard.py are incorrect for EndHtml, StartFragment, and EndFragment, because html_header_size already includes the description_size. The code as it is now makes the EndHtml, StartFragment, and EndFragment values too big. Removing description_size+ from lines 220, 221, and 222 make it work OK.

It was a little challenging to debug because it looks like Excel and other programs are more forgiving of invalid values so it appeared to be an issue with Word in my case.

johnsdouglass avatar May 05 '21 04:05 johnsdouglass

Thanks for your help @johnsdouglass ! I have remove the three description_size+ from lines 220, 221, and 222

  1. When I run
     import klembord
     klembord.set_with_rich_text('plain text', '<b>plain text</b>')

Then I have this error:

File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\klembord\winclipboard.py", line 225, in wrap_html
    *[str(x).zfill(8) for x in sizes]
IndexError: tuple index out of range
  1. After that I tried with
import klembord 
content = {'UTF8_STRING': 'some text'.encode(), 'text/html': '<s>string</s>'.encode()}
klembord.set(content)

Now I don't get any error but nothing is paste.

MagTun avatar May 05 '21 16:05 MagTun

Hmm. I just tried your example and it works fine for me.

Can you verify that lines 218-233 look like this:

		sizes = (
			description_size, # StartHtml
			html_header_size+fragment_size+html_footer_size, # EndHtml
			html_header_size, # StartFragment
			html_header_size+fragment_size, # EndFragment
		)

johnsdouglass avatar May 05 '21 21:05 johnsdouglass

Sorry I misunderstood your post. I corrected it and now the klembord.set_with_rich_text is working and I get html on all the programs I tested so that's really great!

But still nothing with the klembord.set(content).

At least there is a way to make it work, so thanks a lot for help!

MagTun avatar May 06 '21 05:05 MagTun