how to check the quantity of the spreadsheets in xlsx workbook in python? Is there any command under win32com library?
Asked
Active
Viewed 597 times
2 Answers
3
You can get the number of spreadsheets in a given workbook by using the popular xlrd module
import xlrd
workbook = xlrd.open_workbook('sample.xlsx')
print len(workbook.sheet_names())
Update per @Gerrat
you can do this too
print workbook.nsheets

Bob Haffner
- 8,235
- 1
- 36
- 43
-
Or just `print workbook.nsheets` (from [here](http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Book.nsheets-attribute)) – Gerrat Jan 06 '15 at 22:55
-
Thanks bob, but it will work only with xls files, not xlsx. – Andrey Pokul Jan 06 '15 at 22:57
2
Here is a link to a mini cookbook for working with win32com and excel.
For your particular question, the following should work:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('your excel file.xlsx')
print(len(wb.Worksheets)) # tells you how many worksheets

Gerrat
- 28,863
- 9
- 73
- 101
-
Great! Thanks! by the way I already found another way. it's simple like wb.Worksheets.Count . Anywayy thanks for reply – Andrey Pokul Jan 06 '15 at 22:57