[英]How to locate and access named ranges (global, nd per-worksheet) using xlrd, Python?
I'm not sure what you are actually reading; 我不确定您实际上正在阅读什么; xlrd access to named ranges has been available for some years now (in version 0.6.0; latest version is 0.7.1) and came with full documentation ab initio. xlrd对命名范围的访问已经可用了几年了(版本0.6.0;最新版本是0.7.1),并且附带了完整的文档,从头开始。
This is the xlrd documentation link that's given on the http://www.python-excel.org/ page that you mentioned. 这是您提到的http://www.python-excel.org/页面上提供的xlrd文档链接 。 Hit PageDown twice and you should see a section headed Named references, constants, formulas, and macros . 点击PageDown两次,您应该看到标题为“引用,常量,公式和宏”的部分 。 This gives an overview and points you to documentation of the Book.name_*
methods & the Name
object, and to a demonstration script. 这给出了一个概述,并指向Book.name_*
方法和Name
对象的文档以及一个演示脚本。
Note that this is the SVN trunk version of the documentation and applies to a future release; 请注意,这是本文档的SVN干线版本,适用于将来的发行版。 it may mention one extra convenience method that's not available in the current released version of xlrd (which you can get from PyPI ) and which includes the relevant documentation file. 它可能会提到一种额外的便捷方法,该方法在当前发布的xlrd版本(您可以从PyPI中获得)中不可用,并且包括相关的文档文件。
Update in response to """I got this far: someRange = book.name_map[u'somerange'][0] and now I want to iterate over it, grab values, get its dimensions, etc. Now what do I do? I tried dir(someRange) and help(someRange) and it has not helped much.""" 更新以响应“”,我到此为止:someRange = book.name_map [u'somerange'] [0],现在我想对其进行迭代,获取值,获取其尺寸等。现在该怎么办?我尝试了dir(someRange)和help(someRange),但并没有太大帮助。“”“
What you are calling someRange
is an instance of the Name
class. 您所说的someRange
是Name
类的实例。 You need to read the documentation of that class . 您需要阅读该类的文档 。 It would help if you were to read the demonstration script xlrdnameAPIdemo.py and try running it over your xls file(s). 如果您要阅读演示脚本xlrdnameAPIdemo.py并尝试在您的xls文件上运行它将很有帮助 。 Note that "get its dimensions" logically precedes "iterate over it, grab values"; 请注意,“获取尺寸”在逻辑上先于“迭代,获取值”; the convenience method Name.area2d
may be what you need. 您可能需要便捷的方法Name.area2d
。
It's not trivial and it worked in my case only on XLS no XLSX (probably because on my XLSX name.evaluated == 0
): 这并非微不足道,它在我的情况下仅适用于XLS而不是XLSX(可能是因为我的XLSX name.evaluated == 0
):
name = book.name_map['my_named_range'][0]
assert name.result.kind == xlrd.oREF
ref3d = name.result.value[0]
for sheet_index in range(ref3d.shtxlo, ref3d.shtxhi):
sheet = book.sheet_by_index(sheet_index)
for row in range(ref3d.rowxlo, min(ref3d.rowxhi, sheet.nrows)):
for col in range(ref3d.colxlo, min(ref3d.colxhi, sheet.ncols)):
cell = sheet.cell(row, col)
# TODO: Do something with that cell.
You want to limit the number of columns and rows in the sheet in case your range is like A:A
or 1:1
(ie, the entire row or column). 您希望限制工作表中的列和行数,以防您的范围是A:A
或1:1
(即整个行或列)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.