如何使用 xlrd、Python 查找和访问命名范围(全局、每个工作表)?

发布于 2024-09-08 02:46:11 字数 155 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2024-09-15 02:46:11

我不确定你到底在读什么; 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) 但没有多大帮助。"""

你所说的 someRangeName< /代码> 类。您需要阅读 文档该类的。如果您阅读 演示脚本 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 & the Name 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 the Name 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 method Name.area2d may be what you need.

吐个泡泡 2024-09-15 02:46:11

这不是微不足道的,它在我的情况下仅在 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.

您想要限制工作表中的列数和行数,以防万一您的范围类似于 A:A1:1 (即整个行或列)。

It's not trivial and it worked in my case only on XLS no XLSX (probably because on my 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 (i.e., the entire row or column).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文