[Python] Python3 Saying Value Name is in workbook.defined_names When it Isn't

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a bit confused as to what is going on, as the name Address is not in workbook.defined_names (I have checked the workbook in Excel as well) but the code passes the if statement if name in workbook.defined_names and fires dests = workbook.defined_names[name].destinations I then get an error for line for title, coord in dests: of NameError: name 'dests' is not defined.

    from openpyxl import load_workbook

    file = "Test.xlsm"
    workbook = load_workbook(filename=file, data_only=True)

    cells = {}
    defined_names = ["Address","State","Country"]
    for name in defined_names:
    if name in workbook.defined_names:
    dests = workbook.defined_names[name].destinations
    for title, coord in dests:
    cell = workbook[title][coord]
    cells[name] = cell.value

    Continue reading...

Compartilhe esta Página