Get cell color from .xlsx

微笑、不失礼 提交于 2019-11-27 18:52:38

问题


I am using openpyxl to read excel file. I want to get cell color from "xlsx" file. I tried to get color so:

wb = load_workbook('Test.xlsx', data_only=True)
sh = wb[Sheet1]
sh['A1'].fill.start_color.index #Green Color

I get "11L", but i need to get rgb color, how i can do it?


回答1:


Looks like the sheet is using the built-on colour index. The mapping for these is in the source of openpyxl.styles.color

COLOR_INDEX = (
    '00000000', '00FFFFFF', '00FF0000', '0000FF00', '000000FF', #0-4
    '00FFFF00', '00FF00FF', '0000FFFF', '00000000', '00FFFFFF', #5-9
    '00FF0000', '0000FF00', '000000FF', '00FFFF00', '00FF00FF', #10-14
    '0000FFFF', '00800000', '00008000', '00000080', '00808000', #15-19
    '00800080', '00008080', '00C0C0C0', '00808080', '009999FF', #20-24
    '00993366', '00FFFFCC', '00CCFFFF', '00660066', '00FF8080', #25-29
    '000066CC', '00CCCCFF', '00000080', '00FF00FF', '00FFFF00', #30-34
    '0000FFFF', '00800080', '00800000', '00008080', '000000FF', #35-39
    '0000CCFF', '00CCFFFF', '00CCFFCC', '00FFFF99', '0099CCFF', #40-44
    '00FF99CC', '00CC99FF', '00FFCC99', '003366FF', '0033CCCC', #45-49
    '0099CC00', '00FFCC00', '00FF9900', '00FF6600', '00666699', #50-54
    '00969696', '00003366', '00339966', '00003300', '00333300', #55-59
    '00993300', '00993366', '00333399', '00333333', 'System Foreground', 'System Background' #60-64
)

11L corresponds to 0000FF00 (hexadecimal) for which the rgb tuple would be green (0,255,0).




回答2:


I got color from cell so:

wb = load_workbook('Test.xlsx', data_only=True)
sh = wb[Sheet1]
i=sh['A1'].fill.start_color.index #Green Color
Colors=styles.colors.COLOR_INDEX
result = str(Coloros[i])
result= "#"+result[2:]


来源:https://stackoverflow.com/questions/32736419/get-cell-color-from-xlsx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!