感谢@Vyassa提供所有正确的指针,我已经能够编写以下代码,该代码在XLS文件中的行上进行迭代,并输出带有“单个”样式信息的单元格的样式信息(例如,整个单元格为斜体)
)或样式“细分”(例如,单元格的一部分是斜体,部分不是)。
import xlrd# accessing Column 'C' in this exampleCOL_IDX = 2book = 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)