Use to_clipboard() with the local operating-system clipboard. Practise the same table parsing and text formatting in Colab using the portable examples below.
import pandas as pd
import tempfile
import sqlite3
from pathlib import Path
from io import StringIO
df = pd.DataFrame({"NAME": ["Ravi", "Raju", "Alex"], "ID": [1, 2, 3],
"MATH": [30, 40, 50], "ENGLISH": [20, 70, 41]})
text = df.to_csv(sep=" ")
print(text.rstrip())
restored = pd.read_csv(StringIO(text), sep=" ", index_col=0)
pd.testing.assert_frame_equal(restored, df)Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 70
2 Alex 3 50 41run_desktop = False # Enable only when you want to replace the local clipboard.
if run_desktop:
result = df.to_clipboard(index=False)
assert result is None
print("Copied rows:", len(df))
else:
print("Desktop clipboard write is disabled; use text or file exports.")Expected output
Desktop clipboard write is disabled; use text or file exports.df.to_clipboard(index=False).text = df.to_csv(sep=" ", index=False)
print(text.rstrip())
pd.testing.assert_frame_equal(pd.read_csv(StringIO(text), sep=" "), df)Expected output
NAME ID MATH ENGLISH
Ravi 1 30 20
Raju 2 40 70
Alex 3 50 41df.to_clipboard(index=False, sep=","). Commas may need a spreadsheet import option instead of direct cell paste; tabs are the usual spreadsheet delimiter.text = df.to_csv(index=False, sep=",")
print(text.rstrip())
pd.testing.assert_frame_equal(pd.read_csv(StringIO(text)), df)Expected output
NAME,ID,MATH,ENGLISH
Ravi,1,30,20
Raju,2,40,70
Alex,3,50,41df.to_clipboard(excel=False, index=False).display_text = df.to_string(index=False)
print(display_text)
assert "Ravi" in display_text and "ENGLISH" in display_textExpected output
NAME ID MATH ENGLISH
Ravi 1 30 20
Raju 2 40 70
Alex 3 50 41selected = df.loc[df["MATH"].ge(40), ["NAME", "MATH"]].copy()
text = selected.to_csv(sep=" ", index=False)
print(text.rstrip())
assert pd.read_csv(StringIO(text), sep=" ")["NAME"].tolist() == ["Raju", "Alex"]Expected output
NAME MATH
Raju 40
Alex 50amounts = pd.DataFrame({"id": ["001", "002"], "amount": [5.25, None]})
text = amounts.to_csv(sep=" ", index=False, na_rep="NA", float_format="%.2f")
print(text.rstrip())
checked = pd.read_csv(StringIO(text), sep=" ", dtype={"id": "string"})
assert checked["id"].tolist() == ["001", "002"]
assert checked["amount"].isna().sum() == 1Expected output
id amount
001 5.25
002 NAconnection = sqlite3.connect(":memory:")
try:
df.to_sql("student", connection, index=False, if_exists="replace")
rows = pd.read_sql("SELECT * FROM student ORDER BY ID LIMIT 5", connection)
finally:
connection.close()
text = rows.to_csv(sep=" ", index=False)
print(text.rstrip())
pd.testing.assert_frame_equal(rows, df)
# Local Python, when clipboard access is available:
# rows.to_clipboard(index=False)Expected output
NAME ID MATH ENGLISH
Ravi 1 30 20
Raju 2 40 70
Alex 3 50 41imported.to_clipboard(index=False). In Colab, print or download tab-separated text. See read_excel() and read_csv().with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
csv_file = Path(folder) / "student.csv"
excel_file = Path(folder) / "student.xlsx"
df.to_csv(csv_file, index=False)
df.to_excel(excel_file, index=False, engine="openpyxl")
for imported in (pd.read_csv(csv_file), pd.read_excel(excel_file, engine="openpyxl")):
pd.testing.assert_frame_equal(imported, df)
assert imported.to_csv(sep=" ", index=False) == df.to_csv(sep=" ", index=False)
print("CSV and Excel source previews passed")Expected output
CSV and Excel source previews passedColab cannot directly write to your Windows clipboard. A missing desktop backend can raise PyperclipException. Avoid copying sensitive tables accidentally and remember that clipboard contents can be replaced. For large transfers use a file. Exporting spreadsheet-ready text does not validate how every spreadsheet will interpret dates, zeros or formula-like strings; set destination types appropriately.
answer = df.loc[df["ENGLISH"].ge(40), ["NAME", "ENGLISH"]].copy()
text = answer.to_csv(sep=" ", index=False)
print(text.rstrip())
assert pd.read_csv(StringIO(text), sep=" ")["NAME"].tolist() == ["Raju", "Alex"]
# Local Python only: answer.to_clipboard(index=False)
# Colab file alternative:
# answer.to_csv("selected.tsv", sep=" ", index=False)
# from google.colab import files
# files.download("selected.tsv")Expected output
NAME ENGLISH
Raju 70
Alex 41Open in Google Colab View on GitHub
Run examples in order. Portable examples create their own files; desktop clipboard calls are disabled by default. Save a copy to keep edits.
Reference: Pandas to_clipboard documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.