开发者

pywin32 excel find how many sheets are in a workbook and index by number?

开发者 https://www.devze.com 2023-03-11 07:42 出处:网络
How do I find the number of sheets in a workbook in pywin32? Also, is there ANY documentation on how to use pywin32 with excel? I can\'t seem to开发者_开发知识库 even find code examples or anything.

How do I find the number of sheets in a workbook in pywin32?

Also, is there ANY documentation on how to use pywin32 with excel? I can't seem to开发者_开发知识库 even find code examples or anything.


from win32com.client import Dispatch

xl= Dispatch("Excel.Application")
xl.Visible = True # otherwise excel is hidden

# newest excel does not accept forward slash in path
wb = xl.Workbooks.Open(r'U:\Example.xls')
print "count of sheets:", wb.Sheets.Count
for sh in wb.Sheets:
    print sh.Name
wb.Close()
xl.Quit()

Result:

count of sheets: 3
Sheet1
Sheet2
Sheet3

Your best documentation is found provided with Excel. Generally, I record a macro, look at the generated code, learn from the help file, and write what I need in Python.

0

精彩评论

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