在 Python 中使用 XLRD 从 Excel 中读取。
简单的场景。我有一个带有值的单元格,它与一个命名范围相关联。
NamedRange "Foo" = Sheet1!$A$1 A1 中的值为 "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??
我只想在 python 代码中引用命名范围“Foo”并打印出单元格的值“Bar”。
编辑:这是另一个更完整的例子:
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 是为了确保 excel 可以读取文件。在我的例子中,命名单元格是 Sheet1 单元格 A1 中的“销售”。
回报:
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'