Python 如何使用openpyxl通过DefinedName获取单元格内容?

Python 如何使用openpyxl通过DefinedName获取单元格内容?,python,openpyxl,Python,Openpyxl,例如,单元格的坐标为A1,使用coordinate='A1'设置一个DefinedName=“cat”。然后我想通过DefinedName“cat”读取单元格中的内容。但它似乎不受支持。还有其他的帮助方法吗 我是如何找到答案的 from openpyxl import load_workbook wb = load_workbook(filename="test.xlsx") # get DefinedNameList instance defined_names_cat = wb.defin

例如,单元格的坐标为A1,使用
coordinate='A1'
设置一个
DefinedName=“cat”
。然后我想通过
DefinedName“cat”
读取
单元格中的内容。但它似乎不受支持。还有其他的帮助方法吗


我是如何找到答案的

from openpyxl import load_workbook
wb = load_workbook(filename="test.xlsx")

# get DefinedNameList instance
defined_names_cat = wb.defined_names['cat']

# get destinations which is a generator
destinations_cat = defined_names_cat.destinations  
values = {}
for sheet_title, coordinate in destinations_cat:
    values.update({sheet_title: wb[sheet_title][coordinate].value})
my_value = values.get('Sheet1')
我不知道excel的功能。因此,我将单元格A1“命名”为
cat
,并保存文件。我将该文件提取为rar文件<在
xl/workbook.xml
中找到了code>cat
,原始内容是
工作表1!$一美元
。它是一个名为
definedName
的元素,具有属性
name
,其内容由工作表的
标题
和单元格
坐标
组成。因此,该函数被称为
defined name
或其他相关函数。我在一个房间里找到的

回答

from openpyxl import load_workbook
wb = load_workbook(filename="test.xlsx")

# get DefinedNameList instance
defined_names_cat = wb.defined_names['cat']

# get destinations which is a generator
destinations_cat = defined_names_cat.destinations  
values = {}
for sheet_title, coordinate in destinations_cat:
    values.update({sheet_title: wb[sheet_title][coordinate].value})
my_value = values.get('Sheet1')

是特定单元格(如A1)或整个表的“cat”别名1-什么是excel的等效引用?cat是单元格的别名,在excel中表示坐标“A1”等于cat。不清楚您所说的别名是什么意思。如果您指的是某种已定义的名称,则这些是工作簿常量。当您在excel中选择一个单元格时,在左上角,它将显示该单元格的坐标,如“A1”、“B8”等。您可以将“A1”更改为另一个名称,如“cat”、“dog”。因此这里的
别名
表示单元格坐标的另一种定义。Excel使用已定义的名称。请看我之前的评论。嗨,爱生霍米达利,在我尝试了代码之后,它工作了。这是我想要的,太棒了,非常感谢@Eric.K.K我是新手,我很乐意帮忙,我也很高兴!