2012-09-11 60 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

這是一個有點手動,但我認爲這是唯一的工作 –