获取熊猫中Excel单元格的背景色read_excel?
问题描述:
我有一个Excel文件,其中的单元格具有背景色.我正在用read_excel
将该文件读取到 pandas 中.有什么方法可以获取细胞的背景色?
I have an 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?
答
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...