简体   繁体   中英

Need to maximize the excel window after opening it using win32com.client

I am opening the excel sheet using win32com.client and bringing window to foreground using Activate function. But the window is opening in minmized view. I need to maximize it. Please help

 import win32com.client as win32

 excel = win32.gencache.EnsureDispatch('Excel.Application')
 wb1 = excel.Workbooks.Open(r'C:\\blp\\1700.xlsx')
 wb2 = excel.Workbooks.Open(r'C:\\blp\\Book1.xlsx')
 excel.Visible = True

 wb1.Activate()

Please let me know how to maximize the Excel window. Thanks!

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'Path...\YourFile.xlsx')
excel.Visible = 1
excel.WindowState = win32.constants.xlMaximized  # this works for me 

Also when you prefix a string path with r'' you do not need to escape the slashes.

Update: If you want to see what COM constants are available then you need to use win32.gencache.EnsureDispatch() once because it will generate a file which contains all the constants from a type library in an object called win32com.clients.constants. After you ran the above line of code then a folder is created in your operating system's temp directory. For example on Windows the temp directory is located at C:\\Users\\'UserName'\\AppData\\Local\\Temp . There will be a folder in there named gen_py . On my system, the available constants you can use are found inside gen_py\\3.7\\00020813-0000-0000-C000-000000000046x0x1x7\\__init__.py . Note that folder 3.7 is the current Python version that you are using so this may vary for you and the following folder that is named with numbers may also be different. After opening the __init__.py file the available constants are found inside the class named constants .

This is how I answered your qestion actually. The Excel Maximize option was in the class constants as xlMaximized =-4137 # from enum XlWindowState .

Also after the gen_py folder is created in the temp directory of your operating system you can use the constants with win32.Dispatch or win32.DispatchEx then but if the aforementioned folder doesn't exist yet then you must use win32.gencache.EnsureDispatch() once to create it.

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