简体   繁体   中英

Open the same excel file in different windows with python

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)

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,.......)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM