2012-09-22 4 views
5

실제로 xlrd 모듈 0.8 버전을 사용하고 있지만 배경색, 글꼴 및 셀이 잠겨 있는지 여부와 같은 셀 속성을 읽는 방법을 모르겠습니다. 파이썬에서 Excel 셀 속성을 얻는 방법

나는

import xlrd 
wb = xlrd.open_workbook(...) 
sh = wb.sheet_by_index(...) 
sh.sh._cell_xf_indexes(2, 2) 

그것은 정보를 포맷하면 wb을 읽는 동안 설정 될 필요가 없다는 오류가 발생합니다 사용하려고하지만, 내가 그 매개 변수가 있다면 그것은이 아직 구현되지 않습니다 보여줍니다.

다른 모듈이 있습니까? 또는 어떻게이 모듈 자체가 셀 속성을 읽을 수 있습니까? 버전 0.7.6 xlrd 사용하는 나를 위해

+0

참조 http://stackoverflow.com/questions/7991209/identifying-excel-sheet-cell-color-code-using-xlrd-package –

답변

11

다음 작품 :

from xlrd import open_workbook 

wb = open_workbook('tmp.xls', formatting_info=True) 
sheet = wb.sheet_by_name("1") 
cell = sheet.cell(6, 0) 
print "cell.xf_index is", cell.xf_index 
fmt = wb.xf_list[cell.xf_index] 
print "type(fmt) is", type(fmt) 
print 
print "fmt.dump():" 
fmt.dump() 

fmt는 XF 클래스의 인스턴스입니다; https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#formatting.XF-class

dump() 메서드는 형식에 대한 모든 정보를 인쇄합니다. 여기에 위의 코드의 출력은 다음과 같습니다

cell.xf_index is 497 
type(fmt) is <class 'xlrd.formatting.XF'> 

fmt.dump(): 
_alignment_flag: 1 
_background_flag: 1 
_border_flag: 1 
_font_flag: 1 
_format_flag: 0 
_protection_flag: 0 
alignment (XFAlignment object): 
    hor_align: 1 
    indent_level: 0 
    rotation: 0 
    shrink_to_fit: 0 
    text_direction: 0 
    text_wrapped: 0 
    vert_align: 2 
background (XFBackground object): 
    background_colour_index: 64 
    fill_pattern: 1 
    pattern_colour_index: 17 
border (XFBorder object): 
    bottom_colour_index: 0 
    bottom_line_style: 0 
    diag_colour_index: 0 
    diag_down: 0 
    diag_line_style: 0 
    diag_up: 0 
    left_colour_index: 0 
    left_line_style: 0 
    right_colour_index: 0 
    right_line_style: 0 
    top_colour_index: 56 
    top_line_style: 1 
font_index: 72 
format_key: 0 
is_style: 0 
lotus_123_prefix: 0 
parent_style_index: 0 
protection (XFProtection object): 
    cell_locked: 1 
    formula_hidden: 0 
xf_index: 497 

이 값 중 일부는 통합 문서 wb에 목록의 인덱스입니다. 예를 들어 fmt.font_index은 72이며 wb.font_list[72]Font 클래스 (https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#formatting.Font-class) 클래스의 인스턴스입니다.