Python和Excel通过其名称取代单元格值,而不是通过其坐标
如何通过访问在Excel中创建的单元格名称而不是提供坐标来获取Python 2.7的单元格值?Python和Excel通过其名称取代单元格值,而不是通过其坐标
在这种情况下通过访问而不是D8 “姓”:
IMG:http://www.hosting.universalsite.org/image-excel-EBA2_58B5BC06.png
从文档示例:
my_range = wb.defined_names['my_range'] # if this contains a range of cells then the destinations attribute is not None dests = my_range.destinations # returns a generator of (worksheet title, cell range) tuples cells = [] for title, coord in dests: ws = wb[title] cells.append(ws[coord])
假设'Surname'
适用于只有一个细胞可以提取它如下:
from openpyxl import load_workbook
wb = load_workbook(filename='your_workbook.xlsx')
title, coord = next(wb.defined_names['Surname'].destinations)
result = wb[title][coord].value
我得到了:'Traceback(最近调用最后一个): 文件“file.py”,第102行,在
如果我运行'my_range = wb.defined_names ['姓']'它工作,我得到: 参数: comment =无,帮助=无,localSheetId =无,customMenu =无,functionGroupId =无,publishToServer = None,attr_text ='Sheet1!$ D $ 18',workbookParameter = None,hidden = None,function = None,description = None,statusBar = None,shortcutKey = None,vbProcedure = None,xlm = None,name ='Surname' 我仍然需要知道如何调用坐标,现在 – AgFre
我应该写'标题,coord = next(wb.defined_names ['Surna me']。destinations)'而不是'title,coord = next(wb.defined_names ['姓氏'])''。更新答案。 –
你可以发布你使用的代码而不是图片吗? – JCVanHamme
'openpyxl'支持[定义名称](https://openpyxl.readthedocs.io/en/default/defined_names.html)。 –
我现在没有可以显示的代码。 – AgFre