Home » excel » python – How can I get amount of rows in Excel file via xlwt

python – How can I get amount of rows in Excel file via xlwt

Posted by: admin May 14, 2020 Leave a comment

Questions:

How can I get amount of rows in Excel file via xlwt? Is it unable and I should open this file for read via xlrd just for get this info?

How to&Answers:

I don’t think it’s possible with xlwt only. Use xlrd (Sheet.nrows) or xlutils. Example from xlutils docs:

>>> from xlutils.tests.fixtures import make_sheet
>>> sheet = make_sheet((
...           ('X',' ',' ',' ',' '),
...           (' ',' ',' ','X',' '),
...           (' ',' ',' ',' ',' '),
...           ('X',' ',' ',' ',' '),
...           (' ',' ','X',' ',' '),
...           (' ',' ',' ',' ',' '),
...           ))
>>> from xlutils.margins import number_of_good_rows
>>> number_of_good_rows(sheet)
5

Answer:

I know this topic is a bit old but it’s the first google result when lookink for this issue. I’m using python 3.6 with xlwt 1.2.0 and to get worksheet length I use this:

len(ws._Worksheet__rows) #care the simple then double underscore