栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号