How to iterate over all sheets

7,263 views
Skip to first unread message

nycynik

unread,
Nov 14, 2011, 2:21:44 PM11/14/11
to openpyxl-users
Am I confused, or is it that the worksheet iterator is only for
iterating over cells in the worksheet?

Should there be an iterator for all sheets in a workbook?

Eric Gazoni

unread,
Nov 14, 2011, 2:28:46 PM11/14/11
to openpyx...@googlegroups.com
Hi,
you don't need a specific iterator for worksheets actually, you can just
use

>>> wb = Workbook()
>>> for ws in wb.worksheets:
>>> ...

The IterableWorksheet is actually using iterators (actually even
generators, the name did not evolve with the actual implementation) to
parse the xml content on the fly.

Hope this clarifies the situation :)

Cheers,
Eric

Le 14/11/11 20:21, nycynik a �crit :

nycynik

unread,
Nov 14, 2011, 3:07:55 PM11/14/11
to openpyxl-users
Thank you! Yes it does. Would love to add that to the docs somewhere,
to help the next guy, but I really don't know where to start.

However, I did write something for the "Playing with data" section.
I could send the whole tutorial.rst file to you, or update the
repository with this change, but, again, that is really something I
need to understand more about how you like to do things.

--
Accessing one worksheet
+++++++++++++++++++++++

By default the first worksheet has an index of 0. The code below will
access the first worksheet::

>>> wb = Workbook()
>>> ws = wb.worksheets[0]

You can also access the worksheet if you know its name using the code
below::

>>> wb = Workbook()
>>> # first create a sheet to find.
>>> ws = wb.worksheets[0]
>>> ws.title = "range names"
>>> # now find the sheet
>>> cur_sheet = wb.get_sheet_by_name(name = 'range names')


Looping over all worksheets in a document
+++++++++++++++++++++++++++++++++++++++++

If you would like to perform an action on all workseets of the
document, you can use the
worksheet iterator as follows::

>>> wb = Workbook()
>>> for ws in wb.worksheets:
... print ws.title

All worksheets are by default IterableWorksheets.
--

Thanks!
Mike

Eric Gazoni

unread,
Nov 15, 2011, 3:29:09 AM11/15/11
to openpyx...@googlegroups.com
Hello Mike,
thanks for your addition to the documentation, I'll add it quickly. A
small correction though, all sheets are not by default
IterableWorksheets, only when you use the iterators in the load_workbook
function. The rest of the time, they are regular
openpyxl.worksheet.Worksheet.

(see
http://readthedocs.org/docs/openpyxl/en/latest/api.html#openpyxl.reader.excel.load_workbook)

If you feel like adding more documentation, feel free, that's something
we desperately need ;-)

If you want, you can fork the project on bitbucket and send me a pull
request when you're done.
This goes for anyone willing to contribute :)

Cheers,
Eric

Le 14/11/11 21:07, nycynik a �crit :

Marius Tripa

unread,
Apr 14, 2020, 12:37:28 PM4/14/20
to openpyxl-users
But what if you want to loop through specific sheets? Using the index split.

Like this:
for sheet in wb.worksheets[3:27]:
    ws = wb[sheet]
        pass
Is something like this possible? 

Best,
Reply all
Reply to author
Forward
0 new messages