2012-09-11 63 views
5

文本的一个子集的格式使用Python,我需要找到一个给定的Excel工作表单元格是要么粗体或斜体所有子。如何找到在Excel文档细胞

我的问题是与此类似:

Using XLRD module and Python to determine cell font style (italics or not)

..但该解决方案并不适用于我,因为我不能假定相同的格式为持有小区中的所有内容。在一个单元格的值可以是这样的:


1.一些大胆的文字一些普通的文本。 部分斜体文字


有没有办法找到一个范围的字符单元的使用xlrd格式(或任何其他Python的Excel模块)?

回答

3

感谢@Vyassa所有的正确指针,我已经能够编写下面的代码来遍历XLS文件中的行并输出样式具有“单一”样式信息(例如,整个单元格是斜体的)或样式“片段”(例如,部分单元格是斜体的,部分不是)的单元的信息。

import xlrd 

# accessing Column 'C' in this example 
COL_IDX = 2 

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

for row_idx in range(first_sheet.nrows): 
    text_cell = first_sheet.cell_value(row_idx, COL_IDX) 
    text_cell_xf = book.xf_list[first_sheet.cell_xf_index(row_idx, COL_IDX)] 

    # skip rows where cell is empty 
    if not text_cell: 
    continue 
    print text_cell, 

    text_cell_runlist = first_sheet.rich_text_runlist_map.get((row_idx, COL_IDX)) 
    if text_cell_runlist: 
    print '(cell multi style) SEGMENTS:' 
    segments = [] 
    for segment_idx in range(len(text_cell_runlist)): 
     start = text_cell_runlist[segment_idx][0] 
     # the last segment starts at given 'start' and ends at the end of the string 
     end = None 
     if segment_idx != len(text_cell_runlist) - 1: 
     end = text_cell_runlist[segment_idx + 1][0] 
     segment_text = text_cell[start:end] 
     segments.append({ 
     'text': segment_text, 
     'font': book.font_list[text_cell_runlist[segment_idx][1]] 
     }) 
    # segments did not start at beginning, assume cell starts with text styled as the cell 
    if text_cell_runlist[0][0] != 0: 
     segments.insert(0, { 
     'text': text_cell[:text_cell_runlist[0][0]], 
     'font': book.font_list[text_cell_xf.font_index] 
     }) 

    for segment in segments: 
     print segment['text'], 
     print 'italic:', segment['font'].italic, 
     print 'bold:', segment['font'].bold 

    else: 
    print '(cell single style)', 
    print 'italic:', book.font_list[text_cell_xf.font_index].italic, 
    print 'bold:', book.font_list[text_cell_xf.font_index].bold 
2

我不知道,如果你能做到这一点与xlrd,但既然你问任何其他Python的Excel模块:openpyxl不能在1.6.1版本做到这一点。

的富文本获取openpyxl/reader/strings.py重建客场功能get_string()。在该模块中设置第二个包含“原始”字符串的表相对容易。

4

xlrd可以做到这一点。您必须使用kwarg formatting_info=True调用load_workbook(),然后工作表对象将具有属性rich_text_runlist_map,该属性是该单元的字典映射单元格坐标((row, col)元组)到运行列表。一个运行列表是(offset, font_index)对序列,其中offset告诉您在单元格中的字体开始,font_index指标到工作簿对象的font_list属性(工作簿对象是什么由load_workbook()返回),它给你一个Font object描述的属性字体,包括粗体,斜体,字体,大小等。

+0

这是一个有点手动,但我认为这是唯一的工作 –