[python RPA] 3 WAYS TO DO RPA WITH PYTHON

 

python robotic process automation

In this post we’ll cover a few packages for doing robotic process automation with Python. Robotic process automation, or RPA, is the process of automating mouse clicks and keyboard presses – i.e. simulating what a human user would do. RPA is used in a variety of applications, including data entry, accounting, finance, and more.

We’ll be covering pynputpyautogui, and pywinauto. Each of these three packages can be used as a starting point for building your own RPA application, as well as building UI testing apps.

pynput

The first package we’ll discuss is pynput. One of the advantages of pynput is that is works on both Windows and macOS. Another nice feature is that it has functionality to monitor keyboard and mouse input. Let’s get started with pynput by installing it with pip:

1
pip install pynput

Once you have it installed, you can get started by importing the Controller and Button classes. Then, we’ll create an instance of the Controller class, which we’ll call mouse. This will simulate your computer’s mouse to allow you to programmatically click buttons and move the mouse around on the screen.

1
2
3
from pynput.mouse import Button, Controller
 
mouse = Controller()

Next, let’s look at a couple simple commands. To right or left-click, we can use the Button class imported above.

1
2
3
4
5
# left-click
mouse.press(Button.left)
 
# right-click
mouse.press(Button.right)

To double click, you just need to add the number two as the second parameter.

1
mouse.press(Button.left, 2)

We can also move the mouse pointer to a different position by using the move method.

1
2
3
mouse.move(50, -50)
 
mouse.move(100, -200)

pynput can control the keyboard, as well. To do that, we need to import the Key class

1
from pynput.keyboard import Key

To make your keyboard type, you can use the aptly-named keyboard.type method.

1
keyboard.type("this is a test")

As mentioned above, pynput can also monitor mouse movements and keyboard presses. To learn more about that functionality and pynputcheck out this link.

pyautogui

Perhaps the most commonly known package for simulating mouse clicks and keyboard entries is the pyautogui library. pyautogui works on Windows, Linux, and macOS. If you don’t have it installed, you can get it using pip:

1
pip install pyautogui

pyautogui is also straightforward to use. For example, if you want to simulate typing a string of text, just use the typewrite method:

1
pyautogui.typewrite("test pyautogui!")

To left-click your mouse, you can use the click method. To right-click, you can use the rightClick method.

1
2
3
4
5
# left-click
pyautogui.click(100, 200)
 
# right-click
pyautogui.rightClick(100, 200)

Searching for an image on the screen

One of the coolest features of pyautogui is that it can search for an image on the computer screen. This is really helpful if you need to find a particular button to click. You can search for an image by inputting the image file name into the locateOnScreen method. The function returns the topleft coordinate along with the height and width of the identified image.

1
location = pyautogui.locateOnScreen("random_image.png")

pyautogui location image on screen

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

To get the center of identified image, use the center method. Then, you can use the click method to click on the center of the identified image – in this case, a button on the screen.

pyautogui get center of image

1
2
3
center = pyautogui.center(location)
 
pyautogui.click(center)

Sometimes an image may not be found exactly on a screen. In this case, you can add the confidence parameter to locateOnScreen to give Python a confidence level of identifying the image.

1
pyautogui.locateOnScreen("random_image.png", confidence = 0.95)

Taking a screenshot

You can take a screenshot with pyautogui using the screenshot method. Passing a filename will save the screenshot out to that file.

1
s = pyautogui.screenshot("sample_screenshot.png")

It’s also possible to take a screenshot of a specific region, rather than the full screen:

1
pyautogui.screenshot(region = c(0, 0, 100, 200))

pywinauto

On Windows, another option we can look into is the pywinauto library. The main disadvantage of this library is that it does not work on macOS or Linux. However, it also offers a couple of nice advantages for Windows users. One, it’s syntax is object-oriented – it’s made to be more Pythonic. Secondly, because of its design, the library can make it easier to perform certain tasks, like clicking on specific buttons or finding menu items in an application.

For example, let’s start by launching Notepad, typing some text, and saving the file. We can do that using the code snippet below. Here, we start Notepad by using the Application class. Then, we refer to the Notepad file we just opened by “UnitledNotepad”. We can use the Edit.type_keys to start typing text.

1
2
3
4
5
6
7
8
from pywinauto.application import Application
 
app = Application(backend="uia").start("notepad.exe")
app.UntitledNotepad.Edit.type_keys("Starting notepad...")
app.UntitledNotepad.menu_select("File->SaveAs")
sub_app=app.UntitledNotepad.child_window(title_re = "Save As")
sub_app.FileNameCombo.type_keys("test_file.txt")
sub_app.Save.click()

Learn more about pywinauto by checking out this link.

Conclusion

That’s it for this post! We covered three packages for doing robotic process automation with Python. 

[출처] https://theautomatic.net/2020/12/17/3-ways-to-do-rpa-with-python/

 

 

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
64 [python][자료구조] "pdf 취소선 검출" How to identify strike-out text from PDF files using Python 졸리운_곰 2024.07.18 544
63 [python][자료구조] 오픈 소스 Python PDF 파서 라이브러리 file 졸리운_곰 2024.07.16 444
62 [python][자료구조] 벡터화 | Vectorization file 졸리운_곰 2023.12.21 330
61 [python][자료구조] Python의 Loop? NO! , Python의 Vectorization? OK!!! file 졸리운_곰 2023.12.21 473
60 [python][자료구조] Python - MySQL 데이터 추가, 삭제, 업데이트 졸리운_곰 2023.05.13 488
59 [python][자료구조] Python의 JSON - 문자열을 JSON으로 변환하는 방법 졸리운_곰 2023.05.06 456
58 [python][자료구조] [Python] Logging to MongoDB (로그 남기기) file 졸리운_곰 2023.03.24 428
57 [python][자료구조] [스파르타 웹 개발 종합] 파이썬으로 크롤링하고 DB에 저장하기(request, bs4, mongoDB 패키지 사용) 졸리운_곰 2023.03.12 461
56 [python][자료구조] [MongoDB] Document Query(조회) – find() 메소드 졸리운_곰 2023.03.08 315
55 [python][자료구조] Python(flask)으로 mongoDB 제어하기 – pymongo file 졸리운_곰 2023.03.08 604
54 [python][자료구조] django sqlite3 MySQL로 전환하기 file 졸리운_곰 2023.03.02 409
53 [python][자료구조] django에서 db.sqlite3 데이터를 mysql로 옮기기 졸리운_곰 2023.03.02 427
52 [python][자료구조] Python - JSON 파일 읽고 쓰는 방법 졸리운_곰 2023.02.04 480
51 [python][자료구조] [인코딩] 유니코드 인코딩 처리 (특히 json 입출력 시) 졸리운_곰 2023.02.04 363
50 [python][자료구조] Dropbox API 사용하기 (with python) 졸리운_곰 2022.12.03 306
49 [Python][자료구조] SQLAlchemy Tutorial(한글) - 2 졸리운_곰 2022.12.03 393
48 [Python]][자료구조] SQLAlchemy Tutorial(한글) - 1 졸리운_곰 2022.12.03 275
47 [python][자료구조] python anaconda 에서 mysql 접속 졸리운_곰 2022.01.25 657
46 [python 자료구조] 림코딩의 파이썬으로 csv 다루기 강좌 (읽기,쓰기,수정,추가) 졸리운_곰 2022.01.16 521
45 python - 읽은 후 kafka 메시지를 삭제하는 방법 졸리운_곰 2021.07.13 900
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED