开发者

Working with multiple excel workbooks in python

开发者 https://www.devze.com 2023-01-02 20:34 出处:网络
Using win32com, I have two workbooks open. How do you know which one is active? How do you change which one is active?

Using win32com, I have two workbooks open.

  1. How do you know which one is active?
  2. How do you change which one is active?
  3. How can you close one and not the other? (not Application.开发者_运维技巧Quit())


What is your larger goal here? Automate already open excel windows or simply write XLS files? If it's the latter you should use consider using xlwt.

How do you know which one is active?

xl = win32com.client.Dispatch("Excel.Application")
wbOne = xl.Workbooks.Add()
wbTwo = xl.Workbooks.Add()
xl.ActiveWorkbook == wbOne
 False
xl.ActiveWorkbook == wbTwo 
 True

How do you change which one is active?

wbOne.Activate()
xl.ActiveWorkbook == wbOne
 True

How can you close one and not the other? (not Application.Quit())

wbOne.Close()
wbTwo.Close()
0

精彩评论

暂无评论...
验证码 换一张
取 消