python打开excel工作簿,Python:使用Win32 COM Api打开Excel工作簿

I'm using the following code to open and display a workbook within Excel:

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')

wb = excel.Workbooks.Open('my_sheet.xlsm')

ws = wb.Worksheets('blaaaa')

excel.Visible = True

When the File 'my_sheet.xlsm' is already opened, Excel asks me whether I want to re-open it without saving.

How could I check in advance whether the workbook is already opened up and in case it is, just bring it to the front?

EDIT: Found out by now:

if excel.Workbooks.Count > 0:

for i in range(1, excel.Workbooks.Count+1):

if excel.Workbooks.Item(i).Name is 'my_sheet.xlsm':

wb = excel.Workbooks.Item(i)

break

And one more question: My worksheet contains some hea

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值