AceofSpades5757 / clip-util

Package for accessing the clipboard with Python, supporting HTML, RTF, and more.
MIT License
2 stars 0 forks source link

setting HTML not working #28

Closed tardyp closed 7 months ago

tardyp commented 7 months ago

Hello,

Trying to use your python module for setting html clipboard on windows.

using following test code:

from clipboard import Clipboard

with Clipboard() as clipboard:
    clipboard.set_clipboard("<html><body><b>hello world</b><body></html>", 'html')
    print(clipboard.available_formats())

with Clipboard() as clipboard:
    print(clipboard['html'])

The output is truncated to:

[49418, 0]
<html><body><b>hello w

doing CTRL-V on any app does not output anything

OS Name: Microsoft Windows 11 Entreprise OS Version: 10.0.22621 N/A Build 22621 Python 3.12.2

AceofSpades5757 commented 7 months ago

The HTML code appears to be all wrong. I'm working on a fix for it.

AceofSpades5757 commented 7 months ago

@tardyp Should be good now. Using the getter methods will give the whole HTML template, so I wouldn't advise using that right now. Otherwise, setting the clipboard to HTML seems to work just fine.

The API and everything are still a bit rough, but let me know if there are any other bugs or features you'd find useful.

tardyp commented 7 months ago

Hi, Thanks for the quick turn around and the nice unit tests!

I tested with simple test script:

from clipboard import Clipboard

with Clipboard() as clipboard:
    clipboard.set_clipboard("<b>hello world</b>", 'html')
    print(clipboard.available_formats())

The resulting clipboard works well in JIRA (with Edge), but not in Word or Outlook:

image

image

not sure exactly were else I can test. FWIW, my main usecase is JIRA copy paste from python scripts, as JIRA api is so complicated, doing full automation is not worth it)

AceofSpades5757 commented 7 months ago

No problem. This has to do with the clipboard being able to support multiple types at one time (e.g. text, HTML, and RTF). Normally when you <C-c> to copy, Windows does something special where it will copy different versions to the clipboard that can be supported. When pasting to a web app, it will usually take HTML with no problem. When pasting to a desktop app, especially Microsoft products, it typically wants RTF, or text. I was looking into this while designing the library, but it wasn't clear if that was a possibility. I'll take a look again to see if I can figure out how this can be supported on Windows.

For desktop apps that don't support HTML, you can use RTF. A quick web search shows some limited support for HTML to RTF.

from clipboard import Clipboard

with Clipboard() as clipboard:
    # Don't forget the `r` to allow escape sequences!
    clipboard.set_clipboard(r"{\rtf1\ansi \b hello world \b0 }", "rtf")

image

tardyp commented 7 months ago

Hello, I made some more testing, and I see that I can actually copy paste from chrome to Word, and that the underlying clipboard is indeed html + unicode, and does not go through RTF.

from clipboard import Clipboard

with Clipboard() as clipboard:
    print(clipboard.available_formats())
    for fmt in clipboard.available_formats():
        print(repr(clipboard[fmt]))
poetry run python test.py
[49323, 13]
'Version:0.9\r\nStartHTML:0000000171\r\nEndHTML:0000006046\r\nStartFragment:0000000207\r\nEndFragment:0000006010\r\nSourceURL:https://github.com/AceofSpades5757/clip-util/issues/28\r\n<html>\r\n<body>\r\n<!--StartFragment--><div class="flex-shrink-0 mb-2 flex-self-start flex-md-self-center" style="box-sizing: border-box; flex-shrink: 0 !important; align-self: center !important; margin-bottom: var(--base-size-8, 8px) !important; color: rgb(132, 141, 151); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, &quot;Noto Sans&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span title="Status: Closed" data-view-component="true" class="State State--merged d-flex flex-items-center" style="box-sizing: border-box; align-items: center !important; display: inline-block; border-radius: 2em; font-size: var(--text-body-size-medium, 0.875rem); font-weight: var(--base-text-weight-medium, 500); line-height: var(--control-medium-lineBoxHeight, 1.25rem); padding: 5px var(--control-medium-paddingInline-normal, 0.75rem); text-align: center; white-space: nowrap; background-color: var(--bgColor-done-emphasis, var(--color-done-emphasis)); border: var(--borderWidth-thin, max(1px, 0.0625rem)) solid #0000; color: var(--fgColor-onEmphasis, var(--color-fg-on-emphasis)); margin-right: 8px;">Closed</span></div><div class="mb-2 flex-shrink-0" style="box-sizing: border-box; flex-shrink: 0 !important; margin-bottom: var(--base-size-8, 8px) !important; color: rgb(132, 141, 151); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, &quot;Noto Sans&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="box-sizing: border-box;"></div></div><div class="flex-shrink-0 mb-2 flex-self-start flex-md-self-center" style="box-sizing: border-box; flex-shrink: 0 !important; align-self: center !important; margin-bottom: var(--base-size-8, 8px) !important; color: rgb(132, 141, 151); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, &quot;Noto Sans&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"></div><div class="flex-auto min-width-0 mb-2" style="box-sizing: border-box; flex: 1 1 auto !important; min-width: 0px !important; margin-bottom: var(--base-size-8, 8px) !important; color: rgb(132, 141, 151); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, &quot;Noto Sans&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a class="author text-bold Link--secondary" data-hovercard-type="user" data-hovercard-url="/users/tardyp/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/tardyp" style="box-sizing: border-box; background-color: transparent; color: var(--fgColor-muted, var(--color-fg-muted)) !important; text-decoration: none; font-weight: var(--base-text-weight-semibold, 600) !important;">tardyp</a><span>\xa0</span>opened this issue<span>\xa0</span><relative-time datetime="2024-04-09T13:40:33Z" class="no-wrap" title="Apr 9, 2024, 3:40 PM GMT+2" style="box-sizing: border-box; white-space: nowrap !important;">2 days ago</relative-time><span>\xa0</span>· 4 comments<span>\xa0</span><span data-issue-and-pr-hovercards-enabled="" style="box-sizing: border-box;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">· Fixed by<span>\xa0</span><a href="https://github.com/AceofSpades5757/clip-util/pull/29" data-hydro-click="{&quot;event_type&quot;:&quot;issue_cross_references.click&quot;,&quot;payload&quot;:{&quot;reference_location&quot;:&quot;ISSUE_HEADER&quot;,&quot;user_id&quot;:109859,&quot;issue_id&quot;:2233469720,&quot;pull_request_id&quot;:1815176520,&quot;originating_url&quot;:&quot;https://github.com/AceofSpades5757/clip-util/issues/28/show_partial?partial=issues%2Ftitle&amp;sticky=true&quot;}}" data-hydro-click-hmac="50bccfa6c7dd3d71527fdc4360e0ed8240d98b33d3a0dd1624ae2922661278ed" data-hovercard-type="pull_request" data-hovercard-url="/AceofSpades5757/clip-util/pull/29/hovercard" style="box-sizing: border-box; background-color: transparent; color: var(--fgColor-accent, var(--color-accent-fg)); text-decoration: none;">#29</a></span></span></span></div><!--EndFragment-->\r\n</body>\r\n</html>'
'Closed\r\ntardyp opened this issue 2 days ago · 4 comments · Fixed by #29'
AceofSpades5757 commented 7 months ago

That's very interesting. I'll have to look more into it. I haven't checked, but I'm betting that the format type (integer) that's being used is different than the HTML format type that the library uses. The library actually followed Windows win32 docs at the time and registered a new "HTML Format": CF_HTML = ctypes.windll.user32.RegisterClipboardFormatW("HTML Format")

On multiple format types at the same time, I did some more research on adding support for this and it looked like it had to do with COM objects. I might need to add a new dependency for COM object support or need to write a C extension for this (not sure yet).