简体   繁体   English

使用xlrd访问Python中Excel电子表格的命名范围的交集值?

[英]Accessing the value of the intersection of named ranges of an Excel spreadsheet in Python using xlrd?

In Excel VBA, you can use the Intersect function which returns a range object that is the intersection of the arguments. 在Excel VBA中,您可以使用Intersect函数,该函数返回作为参数交集的范围对象。 You can then get a value from that like so. 然后,您可以像这样从中获取价值。

Intersection("Name1", "Name2").Value

In a standard cell formula you can do the same thing: 在标准单元格公式中,您可以执行以下操作:

=Name1 Name2

How do I accomplish this using xlrd? 如何使用xlrd完成此操作? I've tried reading through the Name class and area2d looks like what I'll be using, but I don't know how to tie this all together. 我尝试阅读Name类,并且area2d看起来像我将要使用的东西,但是我不知道如何将所有这些联系在一起。

As far as I know, xlrd does not natively support this kind of intersection functionality. 据我所知, xlrd本身不支持这种相交功能。 For the kinds of rectangular ranges that you describe, you can fairly easily calculate the intersection though, using the Name class, like you already suggested yourself. 对于您描述的矩形范围,您可以使用Name类很容易地计算出相交,就像您自己已经建议的那样。

The xlrd-0.9.0 distribution that I use with Python 2.7.2 has a directory examples that shows how to work with named ranges. 我在Python 2.7.2使用的xlrd-0.9.0发行版有一个目录examples ,该examples显示了如何使用命名范围。 It comes with the file namesdemo.xls which is a workbook from an older version of Excel (it says version 97-2004 ). 它带有文件名namesdemo.xls ,它是旧版本的Excel(其版本为97-2004 )的工作簿。

An example of using that file to determine the cells in a range called Sales is as follows: 使用该文件确定名为Sales的区域中的单元格的示例如下:

>>> import xlrd
>>> book=xlrd.open_workbook('namesdemo.xls')
>>> nameObj = book.name_and_scope_map.get(('sales', -1))
>>> nameObj.area2d()[1:5]
(1, 2, 1, 14)
>>> nameObj.result.text
u'Sheet3!$B$2:$N$2'

You can parse that last result to obtain the rectangle of cells in your range, or use the elements [1:5] from the tuple returned by the area2d() function -- they represent rowxlo , rowxhi , colxlo and colxhi of the rectangle. 您可以解析最后一个结果以获得您范围内的单元格矩形,或使用area2d()函数返回的元组中的元素[1:5] -它们代表rowxlorowxhicolxlocolxhi Do the same thing for another range and then you can intersect them "manually". 对另一个范围执行相同的操作,然后可以“手动”相交。

Note the lower-case name 'sales' in the call to name_and_scope_map.get() . 注意对name_and_scope_map.get()的调用中的小写名称'sales' That is required because Excel does not have case-sensitive range names. 这是必需的,因为Excel没有区分大小写的范围名称。 The -1 parameter indicates that it should search for the name globally, on all sheets. -1参数表示应在所有工作表上全局搜索名称。

If saved in the newer Excel .xlsx format, the Name object seems to behave slightly differently: 如果以较新的Excel .xlsx格式保存,则Name对象的行为似乎略有不同:

>>> book=xlrd.open_workbook('namesdemo.xlsx')
>>> nameObj = book.name_and_scope_map.get(('sales', -1))
>>> nameObj.formula_text
u'Sheet3!$B$2:$N$2'

In this case, I have not found any way to get access to any actual coordinates via area2d() : 在这种情况下,我还没有找到通过area2d()访问任何实际坐标的任何方法:

>>> nameObj.area2d()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/xlrd/book.py", line 291, in area2d
    self.dump(self.book.logfile,
AttributeError: 'NoneType' object has no attribute 'logfile'

so it looks like you would need to parse the formula_text string or check out the source code of xlrd to see if you can fix it :-) 所以看起来你需要解析formula_text字符串或检查出的源代码xlrd ,看看你是否能:-)修复

The example script xlrdnameAPIdemo.py gives some more explanations, for example about how to look for named ranges in a particular scope/worksheet. 示例脚本xlrdnameAPIdemo.py提供了更多说明,例如有关如何在特定范围/工作表中查找命名范围的说明。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM