HOWTO · Python
How to Copy Text to Clipboard in Python
Learn how to copy Python text to the system clipboard with packages, tkinter, or pandas.
On this page
Python clipboard tools send text to the operating system clipboard so it can be pasted into another application. For plain text, pyperclip offers a compact copy and optional paste API. Other choices include pyperclip3 for byte-oriented workflows, clipboard and xerox as alternatives, tkinter for GUI programs, and pandas for tables. Clipboard access depends on the operating system and the session in which the program runs.
Copy text with pyperclip
Install the package with python -m pip install pyperclip. The short program below runs with Python’s standard library and displays the text value you can pass to pyperclip.copy() in an environment where the package and system clipboard are available.
text_to_copy = "Hello, Clipboard!"
print(text_to_copy)
Hello, Clipboard!
To send that value to the clipboard, use import pyperclip followed by pyperclip.copy(text_to_copy). Use pyperclip.paste() when you also need to read the current clipboard text. Those package calls need a working clipboard mechanism in the current session; some Linux setups also need a desktop session and clipboard utilities.
Use pyperclip3 when you need bytes
Install with python -m pip install pyperclip3 when your code needs byte data from the clipboard. Its copy() and paste() operations handle bytes. Convert values explicitly: use str(number).encode("utf-8") before copying a number, then decode the returned bytes and parse them if needed. A clipboard stores text or bytes, not the original Python object type.
Copy text with the clipboard package
Install the clipboard package with python -m pip install clipboard. The package exposes copy() and paste(); for example, after import clipboard as c, call c.copy("Python Clipboard") and c.paste(). Keep the alias consistent in both calls. This is a text-oriented alternative and still relies on clipboard support from the host environment.
Use xerox as another clipboard package
Install with python -m pip install xerox, then use import xerox, xerox.copy("Python Xerox Example"), and xerox.paste() for text. Platform support and any system dependencies can vary with the installed package version and operating system, so consult its installation guidance if clipboard access is unavailable.
Use tkinter for GUI applications
When a program already has a GUI, tkinter can use the windowing system’s clipboard methods. See how to install tkinter in Python if it is missing. A graphical display is required. Create a root window, then call root.clipboard_clear(), root.clipboard_append(text_to_copy), and root.update() to send text. update() processes Tk events; clipboard ownership and persistence after the program exits depend on the windowing system. GUI programs can keep their event loop active while the user works.
Copy a DataFrame with pandas
For tabular data, install pandas and use DataFrame.to_clipboard(), for example df.to_clipboard(index=False, header=False). This formats a table for pasting into a spreadsheet or another tabular application; it is not a general method for copying arbitrary Python objects. The two arguments omit row labels and column names. Remove either argument to include that part. The call also needs a clipboard mechanism supported by the current environment.