|
| 1 | +import sys |
| 2 | + |
| 3 | + |
| 4 | +def copy_html_to_clipboard(html_content, text_fallback=None): |
| 5 | + """ |
| 6 | + Copy HTML content to clipboard as formatted text with fallback to plain text |
| 7 | +
|
| 8 | + Args: |
| 9 | + html_content (str): HTML content to copy |
| 10 | + text_fallback (str): Plain text fallback (optional) |
| 11 | + """ |
| 12 | + if sys.platform != "win32": |
| 13 | + raise NotImplementedError("This function is only implemented for Windows.") |
| 14 | + |
| 15 | + import win32clipboard |
| 16 | + import win32con |
| 17 | + |
| 18 | + # If no text fallback provided, strip HTML tags |
| 19 | + if text_fallback is None: |
| 20 | + import re |
| 21 | + |
| 22 | + text_fallback = re.sub("<[^<]+?>", "", html_content) |
| 23 | + |
| 24 | + # Prepare HTML clipboard format |
| 25 | + html_clipboard_data = prepare_html_clipboard_data(html_content) |
| 26 | + |
| 27 | + win32clipboard.OpenClipboard() |
| 28 | + try: |
| 29 | + win32clipboard.EmptyClipboard() |
| 30 | + |
| 31 | + # Set HTML format (for rich text paste) |
| 32 | + html_format = win32clipboard.RegisterClipboardFormat("HTML Format") |
| 33 | + win32clipboard.SetClipboardData( |
| 34 | + html_format, html_clipboard_data.encode("utf-8") |
| 35 | + ) |
| 36 | + |
| 37 | + # Set plain text format (for fallback) |
| 38 | + win32clipboard.SetClipboardData(win32con.CF_TEXT, text_fallback.encode("utf-8")) |
| 39 | + |
| 40 | + # Set Unicode text format |
| 41 | + win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text_fallback) |
| 42 | + |
| 43 | + finally: |
| 44 | + win32clipboard.CloseClipboard() |
| 45 | + |
| 46 | + |
| 47 | +def prepare_html_clipboard_data(html_content): |
| 48 | + """ |
| 49 | + Prepare HTML content for Windows clipboard format |
| 50 | + """ |
| 51 | + # HTML clipboard format requires specific headers |
| 52 | + html_prefix = """Version:0.9 |
| 53 | +StartHTML:000000000 |
| 54 | +EndHTML:000000000 |
| 55 | +StartFragment:000000000 |
| 56 | +EndFragment:000000000 |
| 57 | +<!DOCTYPE html> |
| 58 | +<html> |
| 59 | +<head> |
| 60 | +<meta charset="utf-8"> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<!--StartFragment-->""" |
| 64 | + |
| 65 | + html_suffix = """<!--EndFragment--> |
| 66 | +</body> |
| 67 | +</html>""" |
| 68 | + |
| 69 | + # Calculate offsets |
| 70 | + start_html = len(html_prefix.split("\n")[0]) + 1 # After version line |
| 71 | + start_fragment = len(html_prefix) |
| 72 | + end_fragment = start_fragment + len(html_content) |
| 73 | + end_html = end_fragment + len(html_suffix) |
| 74 | + |
| 75 | + # Format the header with correct offsets |
| 76 | + header = f"""Version:0.9 |
| 77 | +StartHTML:{start_html:09d} |
| 78 | +EndHTML:{end_html:09d} |
| 79 | +StartFragment:{start_fragment:09d} |
| 80 | +EndFragment:{end_fragment:09d}""" |
| 81 | + |
| 82 | + full_html = f"""{header} |
| 83 | +<!DOCTYPE html> |
| 84 | +<html> |
| 85 | +<head> |
| 86 | +<meta charset="utf-8"> |
| 87 | +</head> |
| 88 | +<body> |
| 89 | +<!--StartFragment-->{html_content}<!--EndFragment--> |
| 90 | +</body> |
| 91 | +</html>""" |
| 92 | + |
| 93 | + return full_html |
0 commit comments