Automate keyboard and mouse operations with python to streamline daily work [RPA]

What you can learn this time

Automat keyboard and mouse operations with python. Whether it's "Kan * re" or "Castle Pu *", the laps can be automated. Daily work can be streamlined without introducing RPA tools at a high license fee. (I thought I'd actually try to automate the laps, but I'm afraid of the rules, so I'll stop quietly) ~~ You can stay logged in even if you leave your seat by remote work, so you won't notice ... ~~

Get the size of the screen you want to operate

Speaking of packages for automating keyboard and mouse operations in python, pyautogui.

import pyautogui as pa
pa.size()
Size(width=1920, height=1200)

For ease of confirmation, use time to temporarily stop before operation. Since there are times when the screen processing and the browser cannot keep up with the reaction because the input of automatic operation is too fast, use time at your convenience.

Moving the mouse from the origin

Position the upper left corner of the screen as the coordinates (0,0). Use moveTo to move the mouse from (0,0) to the specified position. Specify how many seconds it takes to move with duration.

import time
time.sleep(1)

pa.moveTo(100,100,duration=1)

Know your current location

pa.position()
Point(x=1735, y=157)

Move from current position

MoveRel moves from the current position to the specified position

time.sleep(1)
pa.moveRel(100,100,duration=1)

Click / special click / double click

pa.click(x=1839, y=293, button='left')
pa.leftClick()
pa.rightClick()
pa.doubleClick()

Pushing / releasing the mouse

pa.mouseDown(x=1735, y=157)
pa.mouseUp()

You can drag and drop it by combining it with moveRel.

But there is a special one for drag and drop

pa.dragTo()
pa.dragRel()

screenshot

ss =pa.screenshot()

Recognizes the image and returns the location

Prepare what you want to recognize with screenshots and snipping tool

This time, save the trash can as gomi.png.

image.png

Returns the position that matches the image from the screen

list(pa.locateAllOnScreen('gomi.png'))
[Box(left=16, top=4, width=65, height=81)]

Returns the center position of what was recognized in the image

pa.center(pa.locateAllOnScreen('gomi.png'))

Once you recognize the center, you can execute the shortcut by combining it with a click.

Type in letters

Typewrite after specifying the position of the search bar or notepad in image recognition

pa.leftClick()
pa.typewrite('HHH',1)

Schedule execution

import schedule
import time

def job():
    print("Put click or processing in the function")

Perform job function operations every 10 minutes

schedule.every(10).minutes.do(job)

Other specifications

#Every hour
schedule.every().hour.do(job)
#10 daily:When it reaches 30
schedule.every().day.at("10:30").do(job)
#When it's monday
schedule.every().monday.do(job)
#Specify day and time
schedule.every().wednesday.at("13:15").do(job)

Stop schedule execution

schedule.run_pending()

that's all

You can also press and hold shift or press the windows button as a hotkey

PyAutoGUI’s documentation schedule doc

Recommended Posts

Automate keyboard and mouse operations with python to streamline daily work [RPA]
[Automation] Manipulate mouse and keyboard with Python
I tried to automate internal operations with Docker, Python and Twitter API + bonus
I tried to improve the efficiency of daily work with Python
Fractal to make and play with Python
How to work with BigQuery in Python
To work with timestamp stations in Python
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
I tried to automate sushi making with python
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
Procedure to load MNIST with python and output to png
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
Automatic follow on Twitter with python and selenium! (RPA)
I want to work with a robot in python.
How to get mouse wheel verdict with Python curses
Automate Chrome with Python and Selenium on your Chromebook
Try to automate pdf format report creation with Python
Something to enjoy with Prim Pro (X-Play) and Python
Easy to use Nifty Cloud API with botocore and python
screen and split screen with python and ssh login to remote server
[Python] How to play with class variables with decorator and metaclass
Launch Cloud Datastore Emulator with docker-compose and work with python app
Try to bring up a subwindow with PyQt5 and Python
How to do Bulk Update with PyMySQL and notes [Python]
Try to automate the operation of network devices with Python
[Let's play with Python] Image processing to monochrome and dots
[Blender] How to handle mouse and keyboard events in Blender scripts
[Road to intermediate Python] Enables comparison operations with original classes
Collecting information from Twitter with Python (MySQL and Python work together)
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Get additional data to LDAP with python (Writer and Reader)
How to log in to AtCoder with Python and submit automatically
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
Low Cost RPA with Google APIs and Python -Post Table Data to Slides: Use Case Overview-
Programming with Python and Tkinter
Connect to BigQuery with Python
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Automate python testing with CircleCI
Connect to Wikipedia with Python
Post to slack with Python 3
[Python] Theremin interface with mouse
Switch python to 2.7 with alternatives
Write to csv with Python
Search engine work with python
Automate sushi making with Python
Get keyboard events with python
Streamline web search with python
About Python and os operations
Works with Python and R
Operate Jupyter with REST API to extract and save Python code
Edit Google Spread Sheets with Python (Low-cost RPA case with Google APIs and Python)
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)