如何使用 openpyxl 通过 DefinedName 获取单元格内容?
How to use openpyxl to get the cell content via the DefinedName?
例如单元格坐标为A1,设置DefinedName name="cat"
和coordinate='A1'
。然后我想通过DefinedName "cat"
读取cell
中的内容。但是好像不支持。还有其他方法可以帮助吗? See here for example picture
from openpyxl import load_workbook
wb = load_workbook(filename="test.xlsx")
ws = wb['Sheet1']
cat = ws['cat'].value
我是如何找到答案的
我不知道excel的功能。所以我 'named' 单元格 A1 为 cat
并保存文件。我通过 rar 文件提取文件。 cat
发现于 xl/workbook.xml
,来源内容为 <definedName name="cat">工作表1!$A</definedName>
。这是一个名为 definedName
的元素,属性为 name
,其内容由工作表的 title
和单元格 coordinate
组成。所以该函数被称为 defined name
或相关的东西。我在 official doc
中找到了它
回答
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,设置DefinedName name="cat"
和coordinate='A1'
。然后我想通过DefinedName "cat"
读取cell
中的内容。但是好像不支持。还有其他方法可以帮助吗? See here for example picture
from openpyxl import load_workbook
wb = load_workbook(filename="test.xlsx")
ws = wb['Sheet1']
cat = ws['cat'].value
我是如何找到答案的
我不知道excel的功能。所以我 'named' 单元格 A1 为 cat
并保存文件。我通过 rar 文件提取文件。 cat
发现于 xl/workbook.xml
,来源内容为 <definedName name="cat">工作表1!$A</definedName>
。这是一个名为 definedName
的元素,属性为 name
,其内容由工作表的 title
和单元格 coordinate
组成。所以该函数被称为 defined name
或相关的东西。我在 official doc
回答
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')