Question

The documentation for xlrd here http://www.python-excel.org/ mentions that it is now possible in latest version, but does not say how.

Was it helpful?

Solution

I'm not sure what you are actually reading; xlrd access to named ranges has been available for some years now (in version 0.6.0; latest version is 0.7.1) and came with full documentation ab initio.

This is the xlrd documentation link that's given on the http://www.python-excel.org/ page that you mentioned. Hit PageDown twice and you should see a section headed Named references, constants, formulas, and macros. This gives an overview and points you to documentation of the Book.name_* methods & the Name object, and to a demonstration script.

Note that this is the SVN trunk version of the documentation and applies to a future release; it may mention one extra convenience method that's not available in the current released version of xlrd (which you can get from PyPI) and which includes the relevant documentation file.

Update in response to """I got this far: someRange = book.name_map[u'somerange'][0] and now I want to iterate over it, grab values, get its dimensions, etc. Now what do I do? I tried dir(someRange) and help(someRange) and it has not helped much."""

What you are calling someRange is an instance of the Name class. You need to read the documentation of that class. It would help if you were to read the demonstration script xlrdnameAPIdemo.py and try running it over your xls file(s). Note that "get its dimensions" logically precedes "iterate over it, grab values"; the convenience method Name.area2d may be what you need.

OTHER TIPS

It's not trivial and it worked in my case only on XLS no XLSX (probably because on my XLSX name.evaluated == 0):

name = book.name_map['my_named_range'][0]
assert name.result.kind == xlrd.oREF
ref3d = name.result.value[0]
for sheet_index in range(ref3d.shtxlo, ref3d.shtxhi):
    sheet = book.sheet_by_index(sheet_index)
    for row in range(ref3d.rowxlo, min(ref3d.rowxhi, sheet.nrows)):
        for col in range(ref3d.colxlo, min(ref3d.colxhi, sheet.ncols)):
            cell = sheet.cell(row, col)
            # TODO: Do something with that cell.

You want to limit the number of columns and rows in the sheet in case your range is like A:A or 1:1 (i.e., the entire row or column).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top