10

使用 Python,我需要在给定的 Excel 工作表单元格中查找粗体或斜体的所有子字符串。

我的问题与此类似:

使用 XLRD 模块和 Python 确定单元格字体样式(斜体与否)

..但是该解决方案不适用于我,因为我不能假设单元格中的所有内容都具有相同的格式。单个单元格中的值可能如下所示:


1.一些粗体文本一些普通文本。一些斜体文字


有没有办法使用 xlrd(或任何其他 Python Excel 模块)在单元格中查找一系列字符的格式?

4

3 回答 3

10

感谢@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
于 2016-06-12T15:35:26.323 回答
7

xlrd可以做到这一点。您必须load_workbook()使用 kwarg调用formatting_info=True,然后工作表对象将具有一个属性rich_text_runlist_map,它是将单元格坐标((row, col)元组)映射到该单元格的运行列表的字典。运行列表是一系列(offset, font_index)对,其中offset告诉您字体在单元格中的哪个位置开始,并font_index索引到工作簿对象的font_list属性(工作簿对象是返回的内容load_workbook()),它为您提供描述字体属性的Font 对象,包括粗体、斜体、字体、大小等。

于 2016-04-10T16:56:50.200 回答
3

我不知道您是否可以使用 来做到这一点xlrd,但是由于您询问任何其他 Python Excel 模块:在 1.6.1 版openpyxl 中无法做到这一点。

富文本get_string()openpyxl/reader/strings.py. 在该模块中设置带有“原始”字符串的第二个表相对容易。

于 2013-03-19T15:46:58.210 回答