coloring cells in excel with pandas

前端 未结 2 1454
眼角桃花
眼角桃花 2020-12-03 14:52

I need some help here. So i have something like this

import pandas as pd
path = \'/Users/arronteb/Desktop/excel/ejemplo.xlsx\'
xlsx = pd.ExcelFile(path)
df          


        
相关标签:
2条回答
  • 2020-12-03 15:25

    You can create a function to do the highlighting...

    def highlight_cells():
        # provide your criteria for highlighting the cells here
        return ['background-color: yellow']
    

    And then apply your highlighting function to your dataframe...

    df.style.apply(highlight_cells)
    
    0 讨论(0)
  • 2020-12-03 15:45

    I just had this same problem and I just solved it this week. My problem was not getting the includes to work properly to get the online code that I found working properly.

    I am going to assume you mean change the background color not change the font color. If I am wrong clarify your request.

    My solution is tied to a particular library. openpyxl

    #### This import section is where my mistake was at
    #### This works for me
    import openpyxl    ### Excel files 
    from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
    from openpyxl.styles import Fill, Color
    from openpyxl.styles import Style
    from openpyxl.styles.colors import RED
    from openpyxl.styles.colors import GREEN
    
    
    str_xls_PathFileCurrent = str_xls_FileName
    ### Opens Excel Document
    var_xls_FileOpen    = openpyxl.load_workbook(str_xls_PathFileCurrent) 
    ### Opens up the Excel worksheet 
    var_xls_TabName     = var_xls_FileOpen.worksheets[0]                  
    ### Put the spreadsheet tab names into an array 
    ary_xls_SheetNames  = var_xls_FileOpen.get_sheet_names()              
    ### Open the sheet in the file you working on 
    var_xls_TabSheet    = var_xls_FileOpen.get_sheet_by_name(ary_xls_SheetNames[0])
    xls_cell = var_xls_TabSheet['d10']
    
    #### Changes the cell background color 
    xls_cell.style = Style(fill=PatternFill(patternType='solid'
        , fgColor=Color('C4C4C4')))  ### Changes background color 
    
    #### Changes the fonts (does not use style) 
    xls_cell.font = xls_cell.font.copy(color  = 'FFFF0000') ### Works (Changes to red font text) 
    xls_cell.font = xls_cell.font.copy(bold  = True) ### Works (Changes to bold font) 
    xls_cell.font = xls_cell.font.copy(italic= True) ### Works (Changes to Italic Text) 
    xls_cell.font = xls_cell.font.copy(size  =   34) ### Works (Changes Size) 
    
    0 讨论(0)
提交回复
热议问题