I have an .xls Excel file with cells having background colors. I am reading that file into pandas with read_excel. Is there any way to get the background colors of cells?
Get Excel cell background color in pandas read_excel?
·
Answer a question
Answers
Brute-forced it through xlrd, as per Mark's suggestion:
from xlrd import open_workbook
wb = open_workbook('wb.xls', formatting_info=True)
sheet = wb.sheet_by_name("mysheet")
#create empy colormask matrix
bgcol=np.zeros([sheet.nrows,sheet.ncols])
#cycle through all cells to get colors
for row in range(sheet.nrows):
for column in range(sheet.ncols):
cell = sheet.cell(row, column)
fmt = wb.xf_list[cell.xf_index]
bgcol[row,column]=fmt.background.background_colour_index
#return pandas mask of colors
colormask=pd.DataFrame(bgcol)
Yet, there must be a better way thorugh pandas directly...
更多推荐

所有评论(0)