Ankit batra
Ankit batra

Reputation: 31

Resizing Notepad via pywinauto

I need to resize the notepad using python pywinauto library.

I am using the below code to start the notepad:-

from pywinauto import application
app = application.Application()
app.start("Notepad.exe")

Upvotes: 2

Views: 5628

Answers (2)

atl-it
atl-it

Reputation: 21

I use the following code to do the same with Notepad:

from pywinauto import application
app = application.Application()
app.start("Notepad.exe")
app.Notepad.move_window(0,0,400,400)

It is pretty simple and works for me. However, I would like to do this with Excel (and Outlook, Edge and Google Chrome). Does anyone know how to do it with these other applications too? I've hunted around for hours and cannot find how to, but found lots of references to pywinauto on this web site. The applications will open fine, but I cannot move/resize them which is what I really would like to do.

UPDATE: I made up the following code that opens an Excel workbook, moves and resizes it:

import win32gui, win32con, win32api
from win32com.client import DispatchEx # Note - "Dispatch" opens in existing instance of Excel, "DispatchEx" opens in new instance

workbook = 'C:\\Users\\*user*\\Documents\\book1.xlsm'

xl = DispatchEx('Excel.Application')
xl.Visible = 1
try:
    xl.Workbooks.Open(workbook)
except:
    print(workbook + ' not found')
try:
    hwnd = win32gui.FindWindow(None, workbook.split("\\")[-1] + ' - Excel')
    # Move windows to specified location and size
    win32gui.MoveWindow(hwnd, 0, 0, 1920, 1042, True)
    # Maximize window
    #win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
except:
    print("Error moving and resizing " + workbook)

I'm no expert in Python coding, so any improvements are welcome. I'd also like to be able to do this with Edge, Chrome and Outlook. I'll be looking into that next.

Upvotes: 1

Roman
Roman

Reputation: 543

Try this:

from pywinauto import application
app = application.Application()
app.start("Notepad.exe")
dlg_spec = app.window()
dlg_spec.move_window(x=None, y=None, width=200, height=100, repaint=True)

Additionally you can move it if you specify something instead of None in x and y.

More information here and here

Upvotes: 3

Related Questions