Skip to content

Instantly share code, notes, and snippets.

@knu2xs
Created January 29, 2024 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu2xs/b8fdcc130e8fbf88bad127243f39be58 to your computer and use it in GitHub Desktop.
Save knu2xs/b8fdcc130e8fbf88bad127243f39be58 to your computer and use it in GitHub Desktop.
Copy to Clipboard Python
def copy_to_clipboard(string: str) -> None:
"""
Copy a string to the system clipboard.
.. note::
This leans on Pandas, so it will not work unless Pandas is installed.
"""
# ensure Pandas is installed and available
if not importlib.util.find_spec('pandas'):
raise EnvironmentError('copy_to_clipboard requires Pandas. Please ensure Pandas is installed in the environment to use.')
# late import after checking for Pandas
from pandas.io.clipboard import clipboard_set
# use clipboard_set to push to clipboard
clipboard_set(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment