简体   繁体   English

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

[英]How to locate and access named ranges (global, nd per-worksheet) using xlrd, Python?

http://www.python-excel.org/上 xlrd的文档提到,现在可以在最新版本中使用,但没有说明如何使用。

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. 您所说的someRangeName类的实例。 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:A1:1 (即整个行或列)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用xlrd访问Python中Excel电子表格的命名范围的交集值? - Accessing the value of the intersection of named ranges of an Excel spreadsheet in Python using xlrd? 如何使用python xlrd从Excel工作表中检索图像 - How can an image be retrieved from an excel worksheet using python xlrd 在 Python 中使用 xlrd 和 xlwt 编写/创建工作表 - Writing/Creating a worksheet using xlrd and xlwt in Python Python:使用pip安装xlrd后“导入错误:没有名为xlrd的模块” - Python: “ImportError: no module named xlrd” after installing xlrd using pip 如何使用 xlutils、xlwt、xlrd 和 python 删除 excel 文件中的现有工作表 - How to delete an existing worksheet in excel file using xlutils, xlwt, xlrd with python 如何使用regex python查找XX st / nd / rd / th - How to locate XX st/nd/rd/th using regex python 如何使用xlrd,xlwt和xlutils将“现有”工作表添加到工作簿 - How to add “existing” worksheet to a workbook using xlrd, xlwt and xlutils 如何使用xlrd从工作表列计算变量? - How to calculate variables from worksheet columns using xlrd? Python xlrd命名范围值 - Python xlrd Named Range Value 如何使用 Selenium 和 Python 根据 html 定位带有文本作为注释的按钮? - How to locate the button with text as Comment as per the html using Selenium and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM