I am very new to python. I'm currently trying to open the same instances of one excel-file (Excel 2013) and move opened windows using python, but can't find any info on how to do it. Manually i would just click on "New Window" on "View" tab. If I'll try open it with subprocess, it'll successively open and close windows. Have you got any suggestions? Thanks in advance.
My current code:
import ctypes
import subprocess
import time
import sys
from win32.win32gui import FindWindow, MoveWindow, GetForegroundWindow
user32 = ctypes.windll.user32
x = user32.GetSystemMetrics(78)
y = user32.GetSystemMetrics(79)
p1 = subprocess.Popen(["C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE", "C:\\Users\\user\\Desktop\\python\\TestBook1.xlsx"])
time.sleep(1)
window_handle1 = GetForegroundWindow()
MoveWindow(window_handle1, 0, 0, int(2/3*x), int(0.5*y), True)
p2 = subprocess.Popen(["C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE","C:\\Users\\user\\Desktop\\python\\TestBook1.xlsx"])
time.sleep(1)
window_handle2 = GetForegroundWindow()
MoveWindow(window_handle2, 0, int(0.5*y), int(2/3*x), int(0.5*y), True)
p3 = subprocess.Popen(["C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE", "C:\\Users\\user\\Desktop\\python\\TestBook1.xlsx"])
time.sleep(1)
window_handle3 = GetForegroundWindow()
MoveWindow(window_handle3, int(2/3*x), 0, int(1/3*x), int(y), True)
(sorry for my bad coding in advance)
CodePudding user response:
I don't know python, so likely to be garbage code!
Using the Excel COM object you get programmatic access to the "new window" (among other things).
The Window
object that is returned from Workbook.NewWindow
has a hWnd property (which might be what you need for the MoveWindow) and/or there is also a Top
and Left
property you can use to move the window too.
import win32com
import win32com.client
app = win32com.client.Dispatch("Excel.application")
workbook = app.Workbooks.Open("your workbook")
newwindow = workbook.NewWindow()
MoveWindow(newwindow.hwnd,.......)