简体   繁体   English

使用win32com.client打开excel窗口后需要最大化

[英]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.我正在使用win32com.client打开 Excel 工作表,并使用 Activate 功能将窗口置于前台。 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.请告诉我如何最大化 Excel 窗口。 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.此外,当您使用r''为字符串路径添加前缀时,您无需对斜杠进行转义。

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.更新:如果您想查看哪些 COM 常量可用,那么您需要使用win32.gencache.EnsureDispatch()一次,因为它将生成一个文件,其中包含来自名为 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 .例如,在 Windows 上,临时目录位于C:\\Users\\'UserName'\\AppData\\Local\\Temp There will be a folder in there named gen_py .那里会有一个名为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 .在我的系统上,您可以使用的可用常量位于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.请注意,文件夹 3.7 是您使用的当前 Python 版本,因此这可能因您而异,以下以数字命名的文件夹也可能不同。 After opening the __init__.py file the available constants are found inside the class named constants .打开__init__.py文件后,可以在名为constants的类中找到可用的constants

This is how I answered your qestion actually.这就是我实际上回答您的问题的方式。 The Excel Maximize option was in the class constants as xlMaximized =-4137 # from enum XlWindowState . Excel 最大化选项在类常量中为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.同样在操作系统的临时目录中创建gen_py文件夹后,您可以使用win32.Dispatch or win32.DispatchEx的常量,但如果上述文件夹尚不存在,则必须使用win32.gencache.EnsureDispatch()一次创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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