Get the text in the Clipboard with Python

I needed to have some text that I used often ready to be copied in my blog, so the first thing I though to avoid writing always the same things was to store it in a text file, then I thought to create a batch file to use it as a command to make the file open so that I could copy the text, but I wanted to be even faster than that, so I thought what if the text is copied with a command without having to open the text file and then copy it? I would save a lot of milliseconds 😂

So I thought to use python to put that text in the clipboard and then create a batch file to use it as a command from the cmd.

How to get the clipboard?

It’s simple if we install pyperclip

pip install pyperclip

D:\bat>pip install pyperclip                                                                                                                                        
Collecting pyperclip                                                                                                                                                
  Downloading pyperclip-1.8.2.tar.gz (20 kB)                                                                                                                        
  Installing build dependencies ... done                                                                                                                            
  Getting requirements to build wheel ... done                                                                                                                      
  Preparing metadata (pyproject.toml) ... done                                                                                                                      
Building wheels for collected packages: pyperclip                                                                                                                   
  Building wheel for pyperclip (pyproject.toml) ... done                                                                                                            
  Created wheel for pyperclip: filename=pyperclip-1.8.2-py3-none-any.whl size=11136 sha256=5000bae3a27626eeaf3e0798e09f971adc7b93f4ef8b9c455e05c771051d3741         
  Stored in directory: c:\users\giova\appdata\local\pip\cache\wheels\70\bd\ba\8ae5c080c895c9360fe6e153acda2dee82527374467eae061b                                    
Successfully built pyperclip                                                                                                                                        
DEPRECATION: google-images-search 1.4.6 has a non-standard dependency specifier click>=7.0<=8.1.*. pip 23.3 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of google-images-search or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063                                                                                                   
Installing collected packages: pyperclip                                                                                                                            
Successfully installed pyperclip-1.8.2  

Then you create a .py file with this code:

import pyperclip

pyperclip.copy('text to be copied')
print(pyperclip.paste())

Ok, now just make a .bat file that start this file with

python clip.py

adding the path of the file.

Put this .bat file where your computer have the file that starts like a command. If you do not know how to do read this post where I explained one of the best trick exists.

Read also this:

How I keep my projects organized with batch files

Run your python script from the cmd with its name

Ok. See ya in the next post.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.