Python xlrd命名范围值 [英] Python xlrd Named Range Value
问题描述
使用Python中的XLRD从Excel中读取。
简单的场景。我有一个具有值的单元格,这与一个命名范围相关联。
NamedRangeFoo= Sheet1!$ A $ 1
A1中的值为bar
book = xlrd.open_workbook()
rng = book.name_map [ 'foo'] [0]#小写由于某种原因
打印rng。 #如何打印单元格值吧?
我只想在python代码中引用命名范围Foo,并打印出值Bar 的细胞。
编辑:
这是另一个更完整的例子:
import xlrd
workbook = xlrd.open_workbook('/ path / to / metester.xls')
cell_obj = workbook.name_and_scope_map.get(('sales' ,-1))
#这会打印Sheet1!$ A $ 1
打印cell_obj.formula_text
#这引发了NoneTypeError
打印cell_obj.cell()
formula_text是确保excel可以读取的文件。在我的情况下,命名的单元格是Sheet1,单元格A1中的销售。
返回:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
打印cell_obj.cell()
文件/usr/local/lib/python2.7/dist-packages/xlrd/book.py,第253行,单元格
self.dump (self.book.logfile,
AttributeError:'NoneType'对象没有属性'logfile'
首先,这是小写,如xlrd模块信息( https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966 ):
name_map [#]
从lower_case_name到列表的映射的名称对象列表按范围顺序排序,通常列表中将有一个项目(全局范围)。
你有两个选择,如果你真的只设置一个单元格的名称,那么你使用Name类的'cell'方法(参见文档):
import xlrd
/ pre>
book = xlrd.open_workbook()
Name = book.name_map ['foo'] [0]
print(Name.cell())
控制台:
text:'Bar'
然而,如果你有一个值的整个范围,那么你需要使用area2d方法名称类:
import xlrd
book = xlrd.open_workbook(q1.xls)
名称= book.name_map ['foo'] [0]
Sheet,rowxlo,rowxhi,colxlo,colxhi = Name.area2d()
for i in range(rowxhi):
print .cell(i,0))
控制台:
text:'Bar'
Using XLRD in Python to read from Excel.
Simple scenario. I have a cell with a value and this is associated with a named range.
NamedRange "Foo" = Sheet1!$A$1 The value in A1 is "Bar"
book =xlrd.open_workbook("") rng = book.name_map['foo'][0] # lower case for some reason. print rng.??? # how to print the cell value bar??
I just want to reference the Named range "Foo" in python code and print out the value "Bar" of the cell.
EDIT: Here is another more complete example:
import xlrd workbook = xlrd.open_workbook('/path/to/metester.xls') cell_obj = workbook.name_and_scope_map.get(('sales', -1)) # this does print Sheet1!$A$1 print cell_obj.formula_text # this raises the NoneTypeError print cell_obj.cell()
formula_text is there to ensure excel can read the file. In my case the named cell is "sales" in Sheet1, cell A1.
Returns:
Sheet1!$A$1 Traceback (most recent call last): File "tester.py", line 7, in <module> print cell_obj.cell() File "/usr/local/lib/python2.7/dist-packages/xlrd/book.py", line 253, in cell self.dump(self.book.logfile, AttributeError: 'NoneType' object has no attribute 'logfile'
解决方案Firstly, it is lower case as explained in the xlrd module information (https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966):
name_map [#]
A mapping from lower_case_name to a list of Name objects. The list is sorted in scope order. Typically there will be one item (of global scope) in the list.
You have two options. If you a truely only setting a name for a single cell, then you use the 'cell' method of the Name class (see the docs):
import xlrd book = xlrd.open_workbook("") Name = book.name_map['foo'][0] print(Name.cell())
Console:
text:'Bar'
If you have, however, named an entire range of values, then you need to use the area2d method of the Name class:
import xlrd book = xlrd.open_workbook("q1.xls") Name = book.name_map['foo'][0] Sheet, rowxlo, rowxhi, colxlo, colxhi = Name.area2d() for i in range(rowxhi): print(Sheet.cell(i,0))
Console:
text:'Bar'
这篇关于Python xlrd命名范围值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!