天天看點

python讀取excel字型_如何使用xlrd版本1.1.0讀取excel中的字型和背景色

文檔

您需要使用xf_index來擷取xlrd.formatting.XF對象。而不是使用各種索引從book對象本身擷取資訊。因為大多數實際的樣式資訊(比如顔色)都存儲在書中。所有其他元素都有指向圖書資料清單或字典的索引:

編碼

我想你在找這樣的東西:import xlrd

book = xlrd.open_workbook("sample.xls", formatting_info=True)

def get_front_color(xf):

font = book.font_list[xf.font_index]

if not font:

return None

return get_color(font.colour_index)

def get_back_color(xf):

if not xf.background:

return None

return get_color(xf.background.background_colour_index)

def get_color(color_index):

return book.colour_map.get(color_index)

def get_if_protected(xf):

if not xf.protection:

return False

return xf.protection.cell_locked

sheets = book.sheet_names()

for index, sh in enumerate(sheets):

sheet = book.sheet_by_index(index)

print "Sheet:", sheet.name

rows, cols = sheet.nrows, sheet.ncols

for row in range(rows):

for col in range(cols):

c = sheet.cell(row, col)

xf = book.xf_list[c.xf_index]

print u'{},{}:{:>6}: FRONT: {:>20} | BACK: {:>20} | LOCKED: {}'.format(

row, col, c.value, get_front_color(xf), get_back_color(xf), get_if_protected(xf)

)