![](/img/trans.png)
[英]How to get the text colour of excel cell using xlrd or openpyxl?
[英]Get cell by column name using xlrd
如果我在Excel(.xls)工作表中有两行,比如键值对,是否有办法通过输入xlrd中的键(row0)来获取值(row1)?
例如,如果我有(0,0) = COLOR
和(1,0) = RED
,我该怎么做:
value = sh.col_values("COLOR") ?
我能找到的最接近的是sh.col_values(int)
,但这只允许我输入一个索引。
您必须向下搜索列,直到找到COLOR
然后在下一行中获取值。
from itertools import product
def value_from_key(sheet, key):
for row_index, col_index in product(xrange(sheet.nrows), xrange(sheet.ncols)):
if sheet.cell(row_index, col_index).value == key:
return sheet.cell(row_index+1, col_index).value
value = value_from_key(sheet, 'COLOR')
如果您知道键是偶数行或奇数行,则可以使用xrange(0, sheet.nrows, 2)
或xrange(1, sheet.nrows, 2)
。
编辑:已更新以搜索列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.