7

我正在尝试使用 XLRD 解析 Excel 电子表格中的数据,以确定哪些单元格值是斜体的。该信息将用于设置一个标志,以确定该值是估计值还是报告值。以下是数据示例:

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。现在我需要添加额外的功能来识别斜体单元格值。

在此先感谢您的帮助...

编辑:XLRD 为我提供了我需要的功能;感谢 John Machin 的回答。这是代码:

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
4

2 回答 2

1

我在这里的解决方案是基于“timmorgan”编写的一个类,可以在这里找到。该课程要求您希望操作的 excel 文档处于打开状态。然后创建 excel 文档对象,然后调用返回范围对象的“get_range”方法。然后可以使用此范围对象来获取指定单元格的字体属性。

#--Requires excel document to be open
import pyexcel
book = pyexcel.ExcelDocument(visible=True) #--keeps excel open
cell = 'r171'
r = book.get_range(cell)
val = book.get_value(cell)

print val, r.font.italic, r.font.name
于 2011-07-18T20:21:01.417 回答
0

使用 xlrd(单独使用,而不是使用 pyexcel):

这是指向 python-excel google-group 的主题的链接。这是关于获得字体颜色的,但这可以让你完成 99% 的工作。

于 2011-07-19T12:10:56.497 回答