openpyxl.shared.exc.NamedRangeException: Invalid named range string: "0"

Issue #292 duplicate
Former user created an issue

I get this error when trying to open a particular xlsx file. Others have worked, but this one gives me this error. This is the traceback:

Traceback (most recent call last): File "C:\Users\uidg8525\workspace\XML_Builder\", line 11, in <module> wb = load_workbook(filename+'.xlsx', data_only=True) File "C:\Python27\myLibraries\ericgazoni-openpyxl-e3bec545653f\openpyxl\reader\", line 136, in load_workbook _load_workbook(wb, archive, filename, use_iterators, keep_vba) File "C:\Python27\myLibraries\ericgazoni-openpyxl-e3bec545653f\openpyxl\reader\", line 212, in _load_workbook wb._named_ranges = read_named_ranges(, wb) File "C:\Python27\myLibraries\ericgazoni-openpyxl-e3bec545653f\openpyxl\reader\", line 117, in read_named_ranges destinations = split_named_range(node_text) File "C:\Python27\myLibraries\ericgazoni-openpyxl-e3bec545653f\openpyxl\", line 76, in split_named_range raise NamedRangeException('Invalid named range string: "%s"' % range_string) openpyxl.shared.exc.NamedRangeException: Invalid named range string: "0"

And this is the piece of code that I use to open the file:

from openpyxl import load_workbook import os

filename = raw_input("\nEnter filename: ") filename = os.path.splitext(filename)[0]

wb = load_workbook(filename+'.xlsx', use_iterators = True, data_only=True) ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])

Comments (12)

  1. Ștefan Urziceanu

    What I attached is the XLSX document. I deleted all sheets but the first, because it is a company report. Nevertheless, the error persists with the document in this form. Let me know if you need additional info.

  2. CharlieC

    The problem is caused by the NamedRange


    For which we simply don't have the mechanics to work with.

    Possible solutions:

    • someone provides the code via a pull request for this and similar ranges
    • such ranges are (silently) skipped when reading files

    The second proposal code be included in 1.9.

  3. CharlieC

    At the moment you can only work around this by patching the code yourself to handle the exception (and ignore the relevant ranges).

  4. CharlieC

    As usual, Microsoft's documentation on this kind of range is useless. But it looks like the use case is limited and, therefore, we might be able to handle it at some point.

    It seems there is something called a dynamic range. Assuming this is only way to set a range programmatically then it could be handled but will probably require a new (immutable?) range type.

  5. Ștefan Urziceanu

    Thanks a lot for the prompt answer. I might patch it when I get the time, but for now I'll just hardcode the data that I need. Keep up the good work!

  6. CharlieC

    I hope to have something in 1.9 that can at least skip these kind of ranges with a warning.

    If it were only a case of OFFSET($A$2,0,0,COUNTA($A$2:$A$200),1) then it would probably be quite easy to parse that. However, you've also got the following monster in there:

    <definedName name="Process" localSheetId="9">OFFSET(INDIRECT(ADDRESS(MATCH(INDEX(val1cell,ROW()-2,1),Area,0)+1,2,,,"sheet3")),0,0,COUNTIF(Area,INDEX(val1cell,ROW()-2,1)),1)</definedName>

    And I don't see us adding the mechanics necessary to handle that any time soon.

    In the meantime hardcoding is the only way around the problem.

  7. Ștefan Urziceanu

    Do you have any idea if it would work in PHPExcel? (Since I understand that openpyxl is based on that)

  8. CharlieC

    I've no idea. openpyxl was initially a port but was never kept in sync with PHPExcel - coding styles and customer requirements are too different for this. We do, however, know that the PHPExcel project spent a lot of time working on a formula engine so it is possible that it might work.

    For openpyxl I think the best we can expect in the short term is that you can open the files (but lose the ranges).

  9. Log in to comment