GUI automation in Python

import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
pyautogui.moveRel(None, 10)  # move mouse 10 pixels down
pyautogui.doubleClick()
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)  # use tweening/easing function to move mouse over 2 seconds.
pyautogui.typewrite('Hello world!', interval=0.25)  # type with quarter-second pause in between each key
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')

References
https://pyautogui.readthedocs.io/en/latest/introduction.html

Get the height of Windows Taskbar using Python

To get the work area size (desktop minus taskbar):

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
work_area = monitor_info.get("Work")
print("The work area size is {}x{}.".format(work_area[2], work_area[3]))

To get the taskbar height:

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
monitor_area = monitor_info.get("Monitor")
work_area = monitor_info.get("Work")
print("The taskbar height is {}.".format(monitor_area[3]-work_area[3]))

References
https://stackoverflow.com/questions/4357258/how-to-get-the-height-of-windows-taskbar-using-python-pyqt-win32/51602236

Take Screenshot on Windows Using Python

from PIL import ImageGrab
im = ImageGrab.grab()
im.save('screenshot.png')

Or

import win32gui, win32ui, win32con, win32api
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
bmp.SaveBitmapFile(memdc, 'screenshot.bmp')

Don’t forget to SetProcessDPIAware if necessary

import ctypes

user32 = ctypes.windll.user32
user32.SetProcessDPIAware()

References
https://stackoverflow.com/questions/4589206/python-windows-7-screenshot-without-pil

matplotlib font not found

When you add new fonts to your system, you need to delete your fontList.cache file in order for matplotlib to find them.

The file fontList.cache is located at your Userfolder –> .matplotlib/fontList.cache or ~/.cache/matplotlib/fontList.cache, for Windows that would normally be C:\Users\yourUsername\.matplotlib\fontList.cache

matplotlib.font_manager
matplotlib.font_manager._rebuild()

References
https://stackoverflow.com/questions/26085867/matplotlib-font-not-found
http://andresabino.com/2015/08/18/fonts-and-matplotlib/
https://stackoverflow.com/questions/45877746/changing-fonts-in-matplotlib