bmpinfo = bitmap.GetInfo() bmpstr = bitmap.GetBitmapBits(True) img = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) img.save('window_screenshot.png') pip install pywin32 After installation, you may need to run:
def automate_notepad(): hwnd = win32gui.FindWindow(None, "Untitled - Notepad") if not hwnd: print("Notepad not found") return win32gui
# Set text via clipboard (simpler for large text) text = f"Automated entry at datetime.now()" set_clipboard_text(text) win32gui.SendMessage(edit, win32con.WM_PASTE, 0, 0) bmpinfo = bitmap
# Simulate Ctrl+S to save (requires keybd_event - not shown for brevity) # Or use SendMessage with WM_COMMAND to menu item win32gui