使用 XLRD 模块和 Python 确定单元格字体样式(斜体与否) [英] Using XLRD module and Python to determine cell font style (italics or not)
问题描述
我正在尝试使用 XLRD 解析 excel 电子表格中的数据,以确定哪些单元格值是斜体的.该信息将用于设置一个标志,表明该值是估计值还是报告值.以下是数据示例:
I'm trying to parse data in an excel spreadsheet using XLRD to determine which cell values are italicized. This information will be used to set a flag as to whether the value is an estimated or reported value. Below is an example of the data:
owner_name year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Alachua, city of 1978 17.4 15.7 16.7 18.3 18.9 18.9 19.2 17.4 19.5 19.8 17.1 16.4
Archer, city of 1978 5.6 3.6 4.3 4.5 4.7 4.8 5.3 5.3 5.4 5.6 3.9 2.8
除了尝试一些基本功能以了解如何从电子表格中提取数据之外,我还没有在多大程度上使用 XLRD.现在我需要添加一些额外的功能来识别斜体单元格值.
I have not used XLRD to any great extent, aside from playing around with some of the basic functions to get a feel for how to pull data from the spreadsheet. Now I need to add that extra bit of functionality to identify italicized cell values.
预先感谢您的帮助...
Thanks in advance for your help...
XLRD 为我提供了我需要的功能;感谢约翰·马钦的回答.这是代码:
XLRD provided me with the functionality I need; thanks to John Machin for the answer. Here is teh codez:
import xlrd
book = xlrd.open_workbook('fl_data.xls',formatting_info=True)
sh = book.sheet_by_index(0)
for row in range(0,sh.nrows):
font = book.font_list
cell_val = sh.cell_value(row,1)
cell_xf = book.xf_list[sh.cell_xf_index(row,1)]
print cell_val,font[cell_xf.font_index].italic
推荐答案
使用 xlrd(单独使用,而不是 pyexcel):
Using xlrd (by itself, not with pyexcel):
这是一个 一个主题的链接到 python-excel google-组.这是关于获取字体颜色,但这可以让您完成 99% 的工作.
Here is a link to a topic to the python-excel google-group. It's about getting font colour but that gets you 99% of the way.
这篇关于使用 XLRD 模块和 Python 确定单元格字体样式(斜体与否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!