Copying hyperlinked text to the clipboard in Python pyperclip3

Viewed 147

My program needs to copy into the clipboard some text with hyhperlinks, so that the user can later paste the text (with hyperlinks) into an app (e.g. Word, Slack).

This functionality works in most apps: for example if I copy a piece of text from a browser (or a Word doc, or a Slack message) which contains hyperlinks, when I paste it into another app it preserves the links.

However when I paste this content with pyperclip I only recover the text (there are no hyperlinks).

After some research I think I need to use a binary form of paperclip content, via pyperclip3. But I don't know how to construct the hyperlinked string object.

When I pyperclip3.paste() after copying the string from Word, I obtain what looks like a binary PDF object. Perhaps I need to create a binary PDF with the link and pass it to pyperclip3.copy()? Are there easier ways? Perhaps MIME?

2 Answers

Not sure if it is possible, but it's going to be very hard to use pyperclip to get hyperlinks, maybe even impossible. A possible option is to use Beautiful Soup and richxerox using Mac environment:

from richxerox import *
from bs4 import BeautifulSoup

soup = BeautifulSoup(pasteboard.get_contents(format='html'),'lxml')
links = [i.attrs['href'] for i in soup.find_all('a')]
print(links)

When copying to the clipboard in Windows, the application can choose to make several formats of the data available to an application that wishes to paste the data. I have attached a screenshot of the different formats available when copying from Chrome.

Clipboard contents after copying from chrome

I know PySide6, a Python wrapper around Qt, lets you give the mime-type of the data you are putting into the clipboard. I don't know if there is a similar feature in pyperclip. This feature lets you share html with included hyperlinks directly with another application.

app.clipboard().mimeData().setHtml("""<html>
<body>
<!--StartFragment--><h3 class="s-post-summary--content-title" style="margin: -0.15rem 0px 0.3846rem; padding-top: 0px; padding-right: var(--su24); padding-bottom: 0px; padding-left: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; line-height: var(--lh-md); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI Adjusted&quot;, &quot;Segoe UI&quot;, &quot;Liberation Sans&quot;, sans-serif; font-size: var(--fs-body3); vertical-align: baseline; box-sizing: inherit; display: block; word-break: break-word !important; overflow-wrap: break-word !important; hyphens: auto !important; color: rgb(35, 38, 41); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
<a href="https://stackoverflow.com/questions/73177180/copying-hyperlinked-text-to-the-clipboard-in-python-pyperclip3" class="s-link" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: var(--theme-post-title-font-family); font-size: 17px; vertical-align: baseline; box-sizing: inherit; text-decoration: none; color: var(--theme-post-title-color-hover); cursor: pointer; user-select: auto; word-break: break-word !important; overflow-wrap: break-word !important; hyphens: auto !important;">Copying hyperlinked text to the clipboard in Python pyperclip3</a></h3><div class="s-post-summary--content-excerpt" style="margin-top: calc(var(--su2) * -1); margin-right: 0px; margin-bottom: var(--su8); margin-left: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; line-height: inherit; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI Adjusted&quot;, &quot;Segoe UI&quot;, &quot;Liberation Sans&quot;, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; color: var(--fc-medium); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word !important; overflow-wrap: break-word !important; hyphens: auto !important; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
My program needs to copy into the clipboard some text with hyhperlinks, so that the user</div><!--EndFragment-->
</body>
</html>
""")

Update: Pyperclip "Currently only handles plaintext." So copying hyperlinks isn't an option currently with pyperclip. https://github.com/asweigart/pyperclip#:~:text=Currently%20only%20handles%20plaintext.

Related