How to Read Text From a Clipboard Using read_clipboard?


The read_clipboard() method reads the text in the clipboard of a system and passes it to read_csv() which returns a data frame.

This method uses the pandas.compact.clipboard module internally.

This module is used to carry out any operations with the clipboard. It provides compatibility functions to deal with clipboards in different operating systems.

Also read: Pandas read_csv(): Read a CSV File into a DataFrame

What Is a Clipboard?

The clipboard is a buffer that almost all operating systems provide for short-term storage and transfer between application programs. It is usually unnamed, whose contents are stored in the computer’s RAM.

A clipboard is used to store data that is copied(texts, pictures) temporarily, and the data remains as it is until new data is copied or when the system is shut down.

how can you view your clipboard history?

For Windows

The shortcut to view clipboard history is Windows+V.

Clipboard Image
Clipboard Image in Windows

For mac

The clipboard can be viewed in Finder->Edit->Show Clipboard.

Clipboard Image in Mac
Clipboard Image in Mac

Prerequisites of Read_clipboard

  • Pandas is a python library that is used to work with datasets. It has many built-in functions for cleaning, exploring, and manipulating data
  • For reading or writing any data format, you must have a python IDE and the Pandas library installed in your system
  • Here’s how you can install the Pandas library
    • For Windows:
      • Launch the Command Prompt
      • Enter the following command
        • pip3 install pandas
    • For Ubuntu:
      • Open Terminal
      • Enter the command to install python first
        • sudo apt install python3-pip
      • Enter the command to install pandas
        • pip3 install pandas
    • For Mac OS:
      • sudo pip3 install pandas

Once you have Pandas library installed, you can convert any storage format to a DataFrame.

Reading Data From the Clipboard

The read_clipboard() method takes the text from the clipboard as input and converts it into a string, which is then passed as the input to the read_csv() function.

The syntax for read_clipboard() is as follows:

pandas.read_clipboard(sep='\\s+', **kwargs)

The parameters in the syntax are:

sep: This argument should be a string or a regular expression.

The default value is ‘s+’, which denotes the presence of one or more white space characters.

**kwargs: kwargs stands for keyword arguments. This argument is used to pass additional arguments to the read_csv method. These arguments will be interpreted as key-value pairs and can be used to customize the behavior of the method.

Return type:

A parsed DataFrame object.

The data stored in the clipboard can be of several types.

In this post, we are going to see the working of read_clipboard() on:

  • Excel data.
  • Data from websites or webpages.
  • Text data.

Copying the Excel Data Into the Clipboard.

In this example, we are going to copy excel data and pass it to read_csv().

Let us start with simple excel data.

The excel file has three rows: Name, Age, ID, and three entries.


This data is copied to the system’s clipboard.

TRead_Clipboard Exceldata1
Read_Clipboard Exceldata1

The code to read this data from the clipboard is as follows:

import pandas as pd

import pandas as pd: We use this statement to bring the Pandas library to our environment. The name pd is a standard alias name for this library.

The data in the clipboard image(above) is being read by the method read_clipboar() and is stored in an object called df.

In the next line, we are printing the data frame created.

The data frame that is obtained is given below.


Now that we have a data frame, we need to pass it to the read_csv method to store the data in a CSV format.

But before we do that, we need to first convert the above data frame to CSV using to_csv method.


In the first line, we are using ‘index=False’ to prevent the creation of an extra unnamed column at the beginning of the file.

In the second line, ‘index_col=[0]’ also serves the same purpose.

df.head(): This method is used to print the first five rows of a file. It may not be useful in datasets that have smaller sizes.


The CSV file is stored in the local storage.


Now that we have understood how read_clipboard() works, let us try a little complex excel data.

Consider the Employee dataset.

This data set has hundreds of data, but we only took a few rows.

It has many attributes with important ones like Full Name, Department, Job Title, Hire Date, Annual Salary, and so on.


The code to read from the clipboard is given below.

import pandas as pd
#reading the content from the clipboard 
#printing the data

In the first line, we are importing the Pandas library as pd.

In the next line, we store the data copied to the clipboard in a new variable called df.

Next, we are printing the first few rows of the data frame using df.head

In the following line, we are converting the data frame to a CSV file with the help of to_csv.

We are using read_csv to read the newly created CSV file.

The data frame is obtained as shown below.


The output of to_csv() is a comma-separated value format that is given below.


As observed in the output, the data frame has many NaN values. Now that we have a data frame, we can perform the necessary manipulations and obtain a format that has more readability.

While dealing with dataframes sometimes having a NaN value could be an issue. Here is an article that describes how to replace the NaN value with zero in python.

Copying Data From a Website to the Clipboard

In this example, let us consider a Student dataset copied to a clipboard from a webpage.
The Student table has the following columns:

  • id
  • name
  • class
  • mark
  • gender

We follow the same code that was used in the previous example.

import pandas as pd

The data copied to the clipboard is being read into a variable called dataf in line 2.

Now this data is converted into a CSV file by to_csv.

Using index=False will prevent the creation of an unnamed column in the output.

We are reading the CSV file with the help of read_csv.

df.head is used to print the first five rows of the file.

The output is given below.


The comma-separated values file is stored in the local storage area.


Copying a Plain Text to the Clipboard

In the third example, we are going to consider the plain text as input from the clipboard.

The plain text is not supported by read_clipboard(), we can provide for delimiters in the text so it can be supported by the function.

The text copied to the clipboard is:


The code is the same as in the previous examples.

import pandas as pd

The only difference in reading plain text from the clipboard is that we need to provide a separator or delimiter in the parenthesis of the method.

Here in this code, the separator we provided is ‘\t’, a tab space.

The output is the following data frame.


Finally, the CSV file is given below.



In this post, we have covered what a clipboard is, how we can view the history of the clipboard, the method we use to read any data from the clipboard, and the module it internally uses to carry out these operations.

We have seen how we can copy different types of data to a clipboard(excel,table, plain text), and read it using read_clipboard and then pass the data to read_csv.

Datasets Used In this Post

1. Employee excel table

2. Student excel table


Official Documentation on Pandas read clipboard module.