Skip to content

Instantly share code, notes, and snippets.

@JanChec
Created January 12, 2023 17:39
Show Gist options
  • Save JanChec/47d15b720ad967ff77a8cb9ebc42d9e6 to your computer and use it in GitHub Desktop.
Save JanChec/47d15b720ad967ff77a8cb9ebc42d9e6 to your computer and use it in GitHub Desktop.
Windows 11 pasting from clipboard in Python - unicode text and files
import pathlib
from typing import List
import win32clipboard
def paste() -> str | List[pathlib.Path]:
try:
win32clipboard.OpenClipboard()
try:
return win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
except TypeError:
raw_paths = win32clipboard.GetClipboardData(win32clipboard.CF_HDROP)
return [pathlib.Path(raw) for raw in raw_paths]
finally:
win32clipboard.CloseClipboard()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment