Manipulate the clipboard in Python and paste the table into Excel

I want to operate the clipboard

The clipboard (English: clipboard) is a shared memory area on a computer that can temporarily store data. (From Wikipedia)

When do you want to manipulate the clipboard in Python? You might think that.

For example, consider this situation.

I want to paste a dataframe (table table) processed by Python into Excel. If you have this Excel file on your computer, you can write it directly in Python, but the file is on your company's server ** and it's difficult to access it directly from Python ... ** **

In such a situation, it is convenient to ** copy the dataframe to the clipboard and then manually open and paste the corresponding Excel file **.

Manipulate the clipboard with pyperclip

Use a module called ** pyperclip ** to copy to or paste from the clipboard.

This pyperclip is very simple, pyperclip.copy () and pyperclip.paste () are almost everything.

pyperclip.png

operation function
Copy to clipboard pyperclip.copy()
Paste from clipboard pyperclip.paste()

How to make a clipboard for pasting into Excel in tabular format

For example, if you copy a table in Excel and paste it in another place in Excel, it will be a table properly.

It may be obvious, but do you feel a little strange? How does the clipboard hold this "tabular format"?

Let's check. I have an Excel table like the one below.

pyperclip_excel_before.png

Copy A1 to E6 of this table to the clipboard with Crtl + C. Then, in Python, execute the following to check the contents of the clipboard.

import pyperclip

pyperclip.paste()

Execution result



'Takeo Oi\t Oitakeo\t man\t1960/8/30\t Chiba\r\n Keiko Nagai\t Nagai Keiko\t woman\t1999/5/21\t Kochi prefecture\r\n Mika Mogi\t Mogi Mika\t woman\t1989/3/27\t Saitama prefecture\r\n Takako Nasu\t Nasta Kako\t woman\t1981/9/29\t Hiroshima prefecture\r\n Shoichi Sugiura\t Sugiura Shoichi\t man\t1991/10/31\t Hyogo prefecture\r\n'

You can see that the next column is separated by ** tab (\ t) ** and the next row is separated by ** line feed code (\ r \ n) **.

clipboard2excel.png

In other words, create a character string separated by a tab (\ t) and a line feed code (\ r \ n) like this, copy it to the clipboard with pyperclip.copy (), and then press Ctrl + V to Excel. It can be pasted in tabular format.

let's do it.

Execute as below and copy the character string you want to paste to the clipboard.

pyperclip.copy("Shigeru Sasaki\t Sasaxi gel\t man\t1964/2/13\wakayama prefecture\r\n Kanae Mita\t Mitaka Kanae\t woman\t1979/10/1\t Akita prefecture\r\n")

Then open Excel and paste it ... pyperclip_excel_after.png

You were able to paste it properly as a table!

Recommended Posts

Manipulate the clipboard in Python and paste the table into Excel
Manipulate files and folders in Python
Associate the table set in python models.py
Open an Excel file in Python and color the map of Japan
Use the LibreOffice app in Python (2) Manipulate calc (from macros and externals)
Automatically create word and excel reports in python
About the difference between "==" and "is" in python
Python programming in Excel
The simplest Python memo in Japan (classes and objects)
Receive the form in Python and do various things
Carefully understand the exponential distribution and draw in Python
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python
Find the Hermitian matrix and its eigenvalues in Python
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Output the specified table of Oracle database in Python to Excel for each file
Memorandum (in openpyxl ① copy and paste from another book ② refer to the comparison table)
It is easy to execute SQL with Python and output the result in Excel
Download the file in Python
Find the difference in Python
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
Recursively get the Excel list in a specific folder with python and write it to Excel.
Paste the image into an excel file using Python's openpyxl
I tried programming the chi-square test in Python and Java.
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Stack and Queue in Python
Get the current date and time in Python, considering the time difference
[Tips] Problems and solutions in the development of python + kivy
Unittest and CI in Python
Macro (VBA) that converts the contents selected in the range in Excel into the list initialization code in Python.
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Count the number of Thai and Arabic characters well in Python
New Python grammar and features not mentioned in the introductory book
[Python3] Save the mean and covariance matrix in json with pandas
Get the formula in an excel file as a string in Python
Get the title and delivery date of Yahoo! News in Python
How to display bytes in the same way in Java and Python
The story of Python and the story of NaN
MIDI packages in Python midi and pretty_midi
Getting the arXiv API in Python
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Anaconda and Python version correspondence table
Sorting algorithm and implementation in Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
About dtypes in Python and Cython
In the python command python points to python3.8
Implement the Singleton pattern in Python
Assignments and changes in Python objects
Split iterator into chunks in python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Manipulate OCTA udf filenames in Python
Hashing data in R and Python
Hit the web API in Python
Paste the matplotlib figure on excel
Function synthesis and application in Python
I wrote the queue in Python