Use read_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
pasted = 'NAME\tID\tMATH\tENGLISH\nRavi\t1\t30\t20\nRaju\t2\t40\t30\nAlex\t3\t50\t40\n'
df = pd.read_csv(StringIO(pasted), sep=" ")
print(df)
assert df["NAME"].tolist() == ["Ravi", "Raju", "Alex"]Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40run_desktop = False # Set True only in local Python after copying a table.
if run_desktop:
local_table = pd.read_clipboard(sep=" ")
print(local_table)
else:
print("Desktop clipboard read is disabled; use the pasted-text examples.")Expected output
Desktop clipboard read is disabled; use the pasted-text examples.console = 'NAME ID MATH ENGLISH\nRavi 1 30 20\nRaju 2 40 30\nAlex 3 50 40\n'
simple = pd.read_csv(StringIO(console), sep=r"\s+")
print(simple)
names = pd.read_csv(StringIO('id\tname\n1\tJohn Deo\n'), sep=" ")
assert names.loc[0, "name"] == "John Deo"
print(names)Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
id name
0 1 John Deofunctions_text = 'function—description\nstrlen—Return string length\ntrim—Remove surrounding whitespace\n'
functions = pd.read_csv(StringIO(functions_text), sep="—", engine="python")
print(functions)
assert functions.columns.tolist() == ["function", "description"]Expected output
function description
0 strlen Return string length
1 trim Remove surrounding whitespacerecords = '001\t75\n002\tpending\n'
typed = pd.read_csv(StringIO(records), sep=" ", header=None,
names=["id", "mark"], dtype={"id": "string"}, na_values=["pending"])
print(typed)
assert typed["id"].tolist() == ["001", "002"]
assert typed["mark"].isna().sum() == 1Expected output
id mark
0 001 75.0
1 002 NaNwith tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
csv_file = Path(folder) / "my_clipboard.csv"
excel_file = Path(folder) / "my_clipboard.xlsx"
df.to_csv(csv_file, index=False)
df.to_excel(excel_file, index=False, engine="openpyxl")
pd.testing.assert_frame_equal(pd.read_csv(csv_file), df)
pd.testing.assert_frame_equal(pd.read_excel(excel_file, engine="openpyxl"), df)
print("CSV and Excel round trips passed")Expected output
CSV and Excel round trips passed
table_html = df.to_html(index=False, escape=True)
print(table_html)
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "my_html.html"
target.write_text(table_html, encoding="utf-8")
assert target.read_text(encoding="utf-8") == table_htmlExpected output
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>NAME</th>
<th>ID</th>
<th>MATH</th>
<th>ENGLISH</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ravi</td>
<td>1</td>
<td>30</td>
<td>20</td>
</tr>
<tr>
<td>Raju</td>
<td>2</td>
<td>40</td>
<td>30</td>
</tr>
<tr>
<td>Alex</td>
<td>3</td>
<td>50</td>
<td>40</td>
</tr>
</tbody>
</table>Copy a rectangular table with a consistent delimiter. Avoid merged cells, extra titles and footnotes. sep and engine are parsing choices; changing engine does not repair a missing clipboard service. A local/headless backend may report PyperclipException when unavailable. Colab runs remotely and cannot read your Windows clipboard; paste text or upload a CSV/Excel file instead. Clipboard data is transient, so save useful imports. Verify row counts, headers, dtypes and missing values before analysis. A headerless table needs header=None and names. Regex separators can interact poorly with quoted delimiters; prefer a standard delimiter where possible.
answer = pd.read_csv(StringIO(functions_text), sep="—", engine="python")
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "php_functions.xlsx"
answer.to_excel(target, index=False, engine="openpyxl")
checked = pd.read_excel(target, engine="openpyxl")
assert checked["function"].tolist() == ["strlen", "trim"]
print(checked)Expected output
function description
0 strlen Return string length
1 trim Remove surrounding whitespaceOpen 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 read_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.