|
| 1 | +Rich Clipboard Formats — RTF and CSV/TSV |
| 2 | +======================================== |
| 3 | + |
| 4 | +``rich_clipboard`` added ``CF_HTML`` for rich paste into Word / Outlook, but two |
| 5 | +other cross-application clipboard formats were still missing: |
| 6 | + |
| 7 | +* **RTF** (``"Rich Text Format"``) — the format almost every rich editor accepts |
| 8 | + for styled paste. ``build_rtf`` / ``rtf_to_text`` build and strip RTF control |
| 9 | + words and ``\uNNNN`` / ``\'XX`` escapes in pure Python, with a fully |
| 10 | + unit-testable round-trip. |
| 11 | +* **CSV / TSV** (the registered ``"Csv"`` format Excel reads) — ``rows_to_csv`` / |
| 12 | + ``csv_to_rows`` are a thin, delimiter-parametrised wrapper over the stdlib |
| 13 | + ``csv`` module, so a table can be put on / read off the clipboard. |
| 14 | + |
| 15 | +The codecs are platform-independent and headless-testable; only the actual |
| 16 | +clipboard I/O is Win32 (raising ``RuntimeError`` elsewhere, like the base |
| 17 | +``clipboard`` module), and the byte transfer is a single generic helper shared by |
| 18 | +both formats. Imports no ``PySide6``. |
| 19 | + |
| 20 | +Headless API |
| 21 | +------------ |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + from je_auto_control import (build_rtf, rtf_to_text, rows_to_csv, |
| 26 | + csv_to_rows, set_clipboard_rtf, set_clipboard_csv) |
| 27 | +
|
| 28 | + rtf = build_rtf("Hello\nWorld") # minimal valid RTF document |
| 29 | + rtf_to_text(rtf) # -> "Hello\nWorld" |
| 30 | +
|
| 31 | + rows_to_csv([["a", "b"], ["1", "2"]]) # 'a,b\r\n1,2\r\n' |
| 32 | + csv_to_rows("a,b\r\n1,2\r\n") # [["a", "b"], ["1", "2"]] |
| 33 | +
|
| 34 | + set_clipboard_rtf("Paste me as styled text") # Windows |
| 35 | + set_clipboard_csv([["Name", "Qty"], ["Pen", "3"]], delimiter="\t") # TSV |
| 36 | +
|
| 37 | +``build_rtf`` escapes braces / backslashes, turns newlines into ``\par`` and |
| 38 | +non-ASCII characters into ``\uNNNN?`` escapes (the output is pure ASCII). |
| 39 | +``set_clipboard_rtf`` / ``set_clipboard_csv`` also seed plain text by default so |
| 40 | +plain editors still paste something; ``get_clipboard_rtf`` returns the raw RTF |
| 41 | +string (feed it to ``rtf_to_text``) and ``get_clipboard_csv`` returns rows. |
| 42 | + |
| 43 | +Executor commands |
| 44 | +----------------- |
| 45 | + |
| 46 | +``AC_set_clipboard_rtf`` / ``AC_get_clipboard_rtf`` / ``AC_set_clipboard_csv`` / |
| 47 | +``AC_get_clipboard_csv`` (the sets take ``text`` / ``rows`` + ``delimiter``). They |
| 48 | +are exposed as the matching ``ac_*`` MCP tools (the sets side-effect-only, the |
| 49 | +gets read-only) and as Script Builder commands under **Data**. |
0 commit comments