嘗試win32com.client.GetActiveObject()方法。這是我在我已經寫了一些方便的功能使用,這一項對於Excel:
def Excel(visible=True):
'''Get running Excel instance if possible, else
return new instance.
'''
try:
excel = win32com.client.GetActiveObject("Excel.Application")
print("Running Excel instance found, returning object")
except:
excel = new_Excel(visible=visible)
print("No running Excel instances, returning new instance")
else:
if not excel.Workbooks.Count:
excel.Workbooks.Add(1)
excel.Visible = visible
return excel
new_Excel只是打開Excel應用程序對象的新實例另一個方便的功能。