Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the operation of clipboard by python

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to operate the clipboard with python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, let's talk about the functions you want to achieve:

Copy some specific text to the clipboard by entering a command line argument. This program may be combined with other operations to achieve automatic reply, but my ability at this stage is not enough to achieve such a function. )

Let's take a look at the specific steps and a few things to pay attention to:

1. Add'#!'at the beginning of the python file. Python3 ", about the specific meaning of #!, see the link at the end of the article.

two。 Instructions for creating a batch file (.bat):

First create a text document (.txt) and write the following:

@ py.exe (with spaces!) The absolute path to the file to be created, such as E:Pythonclipboard_copy (with spaces!) % *

@ pause

Be sure to pay attention to spaces when using any command line! Because the parameters in the command line are divided by spaces, any unnecessary spaces may cause misunderstanding in the system, so spaces should never appear in the file path. Can be replaced by an underscore (you must develop a good habit of naming files when programming), and it is best not to appear in Chinese in the file name, specifically because I haven't tested it for coding reasons. But since it will cause trouble, you'd better use English (just to improve your English).

Next, save the text file in .bat file format, as shown in the following figure:

Select Save as (ctrl+shift+S) in the text document interface, and note that not only change the file suffix to .bat, but also select the "all files" type in the save type.

After you save the batch file, move it to the C:Windows directory (or any other path that has been set as an environment variable).

The specific code in the 3.python file:

#! Python3 # TEXT is a dictionary of what you want to copy to the clipboard and access their keywords TEXT = {"busy": "Hello, the subscriber you dialed is busy, if you need a call back. Please leave a message: "," busy ":" Sorry, I "m busy now.", "Socialist core values": "prosperous, strong, democratic, civilized, harmonious, free, equal, fair, rule of law, patriotic, dedicated, honest and friendly"} import sys, pyperclipif len (sys.argv) < 2: # No parameter print ("Sorry, no input has been received yet …") is passed into argv except the file name at this time. Sys.exit () keyphrase = sys.argv [1] if keyphrase in TEXT: pyperclip.copy (TEXT [keyword]) print ("The text" + TEXT [keyphrase] + "is copied to the clipboard") else: print ("There is no such text")

Chinese can appear in the content of the cut-and-paste and the keywords used.

4. How to use it:

Press win+R, open the cmd command window, and type mclip directly into it (this is my file name, the actual situation is determined by your own file name) (space!) In the command in TEXT, you will find that what you need has been copied to the clipboard.

This is the end of the content of "how to operate the clipboard with python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report