When using PyAutogui for screen automation, I found that there was no way to use Write to type Chinese into the input box — which makes sense, since Write is just a wrapper around the keyboard, and what the keyboard can type depends entirely on the input method. But if you want to input something, but also the first operation switch input method, it is too complicated, so I want to use the clipboard copy and paste to achieve.

However, PyAutogui does not have an API for manipulating the clipboard, probably because other libraries have done a good job. This other library is PyPerClip.

(A search also found another library with a more similar name, called Clipboard, which was not maintained for many years – “I just finished this project and saw that PyperClip has all the functionality, ok, I’m a better name, so you can choose” – the project’s home page. Hahaha, love the author for 10 seconds.)

PyPerClip is very simple to use, the common methods are copy and paste two. After testing, the clipboard content can support Chinese perfectly. Here is an example:

Import pyperClip as PC x = "pc.copy(x) a = pc.paste() print(a)"

Output:

This is a piece of text to copy and paste

The installation command for the library is PIP install pyperclip

Note:Paste The paste method doesn’t paste the contents of the clipboard into the current input field. Instead, paste it into a string variable… This is a fake paste! So in order to actually stick to the input box, we have to use the hotkey method in PyAutogui:

Import pyperClip as PC import pyautogui as auto x = "pc.copy(x) auto. Hotky (' Ctrl ',' V ')"