美文网首页
xlrd如何读取Excel单个cell表格的详细格式信息

xlrd如何读取Excel单个cell表格的详细格式信息

作者: cheson00 | 来源:发表于2019-11-14 20:26 被阅读0次

xlrd是Python读取Excel的扩展工具。可以实现指定表单、指定单元格的读取。对Excel的一般读取和数据操作可以参考https://blog.csdn.net/Sukie_csdn/article/details/78672626

这里主要讨论需要获取单个cell单元格详细格式信息的情况。

想要读取格式信息,在打开Excel文档时要将formatting_info设置为True(默认为False),设置为True则会读取Excel文档详细的格式信息,这样程序会消耗更多的内存,所以在不必要的情况下一般设置为False。打开文件代码如下:


book = xlrd.open_workbook('your-file.xls', formatting_info=True)

目前的xlrd模块只支持以formatting_info=True打开xls格式文件,不支持xlsx格式,否则会抛出异常:


raise NotImplementedError("formatting_info=True not yet implemented")

解决的方法是将xlsx文件另存为xls格式文件,切记是另存为而不是将后缀名修改为xls,其他方案参考https://www.cnblogs.com/Detector/p/8709362.html

cell的格式分为以下三种情况,

1.读取cell内容的数据格式信息

主要有7中数据格式,可以利用


sheet.cell_type(rowx,colx)

以及


sheet.cell(rowx,colx).ctype

来获取数据格式信息,参考https://www.cnblogs.com/insane-Mr-Li/p/9092619.html或者https://blog.csdn.net/Sukie_csdn/article/details/78672626,他们的返回值和对应的数据类型如下: 0. empty(空的),1 string(text),2 number,3 date,4 boolean,5 error,6 blank(空白表格)

2.读取cell的表格格式信息

包括cell的背景格式、受保护属性、对齐格式以及边框格式等,当然也可获取字体格式,但这个字体格式是针对cell单元格设置的,并不是针对cell中的文本内容设置的。xlrd中获取cell格式信息主要依赖XF对象实现,获取XF对象的方法如下:


sheet = book.sheet_by_index(0)

xfx = sheet.cell_xf_index(0, 0)

xf = book.xf_list[xfx]

我们可以这样理解以上代码,xlrd在读取文档时已经将cell的格式信息读取,并保存至一个XF对象中,然后将XF对象保存至xf_list列表(该列表保存在打开文件的book对象中)中,通过


cell_xf_index(rowx, colx)

获取该单元格对应的XF对象在xf_list列表中的索引值,通过索引值就可以在xf_list列表中找到该cell对应的XF对象,通过XF对象就可以获取cell的背景格式、受保护属性、对齐格式以及边框格式等信息,例如如下代码:


bgx = xf.background.pattern_colour_index

可以获取cell的背景颜色信息(返回颜色编号)。XF对象有4个可以直接访问的属性值,即protection、background、alignment和border(即受保护属性、背景格式、对齐格式以及边框格式)。

protection有2个属性,源码解释如下:


#: 1 = Cell is prevented from being changed, moved, resized, or deleted

#: (only if the sheet is protected).

cell_locked = 0

#: 1 = Hide formula so that it doesn't appear in the formula bar when

#: the cell is selected (only if the sheet is protected).

formula_hidden = 0

background有3个属性,源码解释如下:


#: See section 3.11 of the OOo docs.

fill_pattern = 0

#: See section 3.11 of the OOo docs.

background_colour_index = 0

#: See section 3.11 of the OOo docs.

pattern_colour_index = 0

alignment有7个属性,源码解释如下:


#: Values: section 6.115 (p 214) of OOo docs

hor_align = 0

#: Values: section 6.115 (p 215) of OOo docs

vert_align = 0

#: Values: section 6.115 (p 215) of OOo docs.

#:

#: .. note::

#:  file versions BIFF7 and earlier use the documented

#:  :attr:`orientation` attribute; this will be mapped (without loss)

#:  into :attr:`rotation`.

rotation = 0

#: 1 = text is wrapped at right margin

text_wrapped = 0

#: A number in ``range(15)``.

indent_level = 0

#: 1 = shrink font size to fit text into cell.

shrink_to_fit = 0

#: 0 = according to context; 1 = left-to-right; 2 = right-to-left

text_direction = 0

border有12个属性,源码解释如下:


#: The colour index for the cell's top line

top_colour_index = 0

#: The colour index for the cell's bottom line

bottom_colour_index = 0

#: The colour index for the cell's left line

left_colour_index = 0

#: The colour index for the cell's right line

right_colour_index = 0

#: The colour index for the cell's diagonal lines, if any

diag_colour_index = 0

#: The line style for the cell's top line

top_line_style = 0

#: The line style for the cell's bottom line

bottom_line_style = 0

#: The line style for the cell's left line

left_line_style = 0

#: The line style for the cell's right line

right_line_style = 0

#: The line style for the cell's diagonal lines, if any

diag_line_style = 0

#: 1 = draw a diagonal from top left to bottom right

diag_down = 0

#: 1 = draw a diagonal from bottom left to top right

diag_up = 0

返回值代表的意义如下:


  0 = No line,

  1 = Thin,

  2 = Medium,

  3 = Dashed,

  4 = Dotted,

  5 = Thick,

  6 = Double,

  7 = Hair,

  8 = Medium dashed,

  9 = Thin dash-dotted,

  10 = Medium dash-dotted,

  11 = Thin dash-dot-dotted,

  12 = Medium dash-dot-dotted,

  13 = Slanted medium dash-dotted.

获取cell表格格式信息后,如何获取cell字体格式信息呢?

实际上,字体信息也是保存在读取文件的book对象中,不过是是保存在了font_list列表中,我们获取cell的字体信息首先要获取cell对应的font_index索引,而font_index也保存在上文提到的XF对象中,于是通过:


book.font_list[xf.font_index].*

即可读取cell表格的字体格式信息,*代表具体的字体格式,包括斜体、粗体以及字体颜色等属性,参考https://xlrd.readthedocs.io/en/latest/api.html#xlrd.formatting.Font。例如:


book.font_list[xf.font_index].italic

可以获取字体是否是斜体,是返回1,不是返回0。

3.cell格式指的是cell内容文本的格式

思考一下,如果cell单元格中的内容前后格式不一样,像下面这样,

image

前面的字体是红色,后面的字体是绿色,如果我们像前文一样通过


book.font_list[xf.font_index].colour_index

获取字体颜色会会返回什么颜色的编号呢?答案是不确定。

如果选中cell框改变字体颜色,像这样

image

或者选中cell中的所有文本,改变字体颜色,像这样

image

然后在像前文那样获取字体颜色,那我们就能获取到红色的编号。

现在们改变部分文本的颜色,像这样

image

或者这样

image

然后我们再像前文那样获取字体颜色,发现还是返回的还是红色的编号,并没有发生改变,这是因为选中cell框或者选中cell中的所有文本都会改变cell的表格格式,而只是选中部分文本改变字体格式并不会改变cell的表格格式,所以获取到的依然是红色,在第二种格式中我也提到了,通过XF对象只能获取到cell的表格格式,那么,要怎样才能获取到cell表格中文本的详细格式呢?我们可以通过:


cell_runlist = table.rich_text_runlist_map.get((rowx, colx)) # 注意是双层括号

返回的cell_runlist对象进行获取,cell_runlist对象是一个二维列表,

cell_runlist[*][0]返回的是文本某种格式起始的位置索引值,

cell_runlist[*][1]返回的是对应起始位置的字体格式对象的索引值,即前文提到的font_index值,

例如还是上文的前面的字体是红色,后面的字体是绿色的文本,

image

获取其cell_runlist对象,

cell_runlist[0][0]的值应为0(红色文本格式的起始位置索引),

cell_runlist[1][0]的值应为2(绿色文本格式的起始位置索引),

book.font_list[cell_runlist[0][1]].colour_index应返回红色编号,

book.font_list[cell_runlist[1][1]].colour_index应返回绿色编号。

xlrd能够读取的具体的字体格式与上文第二种情况提到的字体格式是一样的,可以参考xlrd的API文档,还有那种文本有多种格式重叠的情况,请大家自己尝试。

相关文章

网友评论

      本文标题:xlrd如何读取Excel单个cell表格的详细格式信息

      本文链接:https://www.haomeiwen.com/subject/ouddictx.html