如何在Excel文档单元格中查找文本子集的格式

如何在Excel文档单元格中查找文本子集的格式,第1张

如何在Excel文档单元格中查找文本子集的格式

感谢@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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存