简体   繁体   中英

Python Win32 and excel

I am using WIN32com to use excel. I need to make sure that excel opens up new instances everytime. So if I run this and already have excel open I need it to open a new excel, and with in the script I need it to open a 2nd excel window for file 2 . This is what I am using: I can get it to open but not in new instances.

 import win32com.client
 import os


 x1 = win32com.client.Dispatch("Excel.Application")

 wb1= x1.workbooks.Open("X:\File1.xlsx")
 wb2 = x1.workbooks.Open("X:\File2.xlsm")
 x1.close("X:\File1.xlsx")

You can use DispatchEx to create a new instance of the application.

x1 = win32com.client.DispatchEx("Excel.Application")
x2 = win32com.client.DispatchEx("Excel.Application")
wb1 = x1.Workbooks.Open(.....
wb2 = x2.Workbooks.Open(.....

Roger

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