如何使用 xlrd、Python 查找和访问命名范围(全局、每个工作表)?
xlrd
的文档在这里 http://www.python-excel.org/ 提到现在可以在最新版本中实现,但没有说明如何实现。
The documentation for xlrd
here
http://www.python-excel.org/
mentions that it is now possible in latest version, but does not say how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你到底在读什么; xlrd 对命名范围的访问已经可用多年了(版本 0.6.0;最新版本是 0.7.1),并且从头开始就附带了完整的文档。
这是给出的 xlrd 文档链接在您提到的 http://www.python-excel.org/ 页面上。按两次 PageDown,您应该会看到一个标题为命名引用、常量、公式和宏的部分。这提供了一个概述,并为您指出了
Book.name_*
方法和方法的文档。Name
对象,以及演示脚本。请注意,这是文档的 SVN 主干版本,适用于未来版本;它可能会提到一种额外的便捷方法,该方法在当前发布的 xlrd 版本中不可用(您可以从 PyPI 获取),其中包括相关的文档文件。
更新以响应“”“我到目前为止: someRange = book.name_map[u'somerange'][0] 现在我想迭代它,获取值,获取其尺寸等现在我该怎么办?我尝试了 dir(someRange) 和 help(someRange) 但没有多大帮助。"""
你所说的
someRange
是Name< /代码> 类。您需要阅读 文档该类的。如果您阅读 演示脚本 xlrdnameAPIdemo 将会有所帮助.py 并尝试在您的 xls 文件上运行它。请注意,“获取其维度”在逻辑上先于“迭代它,获取值”;方便的方法
Name.area2d
可能就是您所需要的。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.
This is the xlrd documentation link that's given on the http://www.python-excel.org/ page that you mentioned. Hit PageDown twice and you should see a section headed Named references, constants, formulas, and macros. This gives an overview and points you to documentation of the
Book.name_*
methods & theName
object, and to a demonstration script.Note that this is the SVN trunk version of the documentation and applies to a future release; 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.
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."""
What you are calling
someRange
is an instance of theName
class. 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). Note that "get its dimensions" logically precedes "iterate over it, grab values"; the convenience methodName.area2d
may be what you need.这不是微不足道的,它在我的情况下仅在 XLS 上有效,没有 XLSX (可能是因为在我的 XLSX
name.evaluated == 0
上):您想要限制工作表中的列数和行数,以防万一您的范围类似于
A:A
或1:1
(即整个行或列)。It's not trivial and it worked in my case only on XLS no XLSX (probably because on my XLSX
name.evaluated == 0
):You want to limit the number of columns and rows in the sheet in case your range is like
A:A
or1:1
(i.e., the entire row or column).