此处xlrd
的文档
http://www.python-excel.org/
提到它现在可能在最新版本中,但没有说明如何。
答案 0 :(得分:4)
我不确定你在读什么; xlrd对命名范围的访问已经有几年了(版本0.6.0;最新版本是0.7.1),并且从头开始提供完整的文档。
This is the xlrd documentation link在您提及的http://www.python-excel.org/页面上提供。点击PageDown两次,您应该看到一个标题为命名引用,常量,公式和宏的部分。这给出了概述,并指出了Book.name_*
方法和文档的文档。 Name
对象和演示脚本。
请注意,这是文档的SVN中继版本,适用于将来的版本;它可能会提到一个额外的方便方法,这个方法在当前发布的xlrd版本中没有(你可以从PyPI获得)并且包含相关的文档文件。
更新以回应“”“我到目前为止:someRange = book.name_map [u'somerange'] [0]现在我想迭代它,抓取值,得到它尺寸,等等。现在我该怎么做?我试过dir(someRange)和帮助(someRange)并没有多大帮助。“”“
您呼叫的内容someRange
是Name
类的实例。您需要阅读documentation of that class。如果您阅读the demonstration script xlrdnameAPIdemo.py并尝试在xls文件上运行它会有所帮助。请注意,“获取其维度”逻辑上先于“迭代它,获取值”;方便方法Name.area2d
可能就是您所需要的。
答案 1 :(得分:1)
这不是微不足道的,只在XLS no 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.
如果您的范围类似于A:A
或1:1
(即整个行或列),您希望限制工作表中的列数和行数。