pythonpandasconditional-statementscell-formatting

Is there a method in Pandas to check if a cell is bolded?


I have a column with names. I want to build a list containing all the names from my column that are bolded. Is there a method in Pandas available to do this?

import pandas as pd

df = pd.read_excel("mydatafile.xlsx")
print("Column Headings:")

mylist = []
for i in df.index:
    if df['Names'][i].celltype == bold
        mylist.append(cell)

Solution

  • pandas does not read styles from Excel. You will have to use another library that does. One such library is styleframe (full disclosure, I'm one of the authors of this library).

    Then, using this code

    from styleframe import StyleFrame
    # 'from StyleFrame import StyleFrame' in older versions (< 3.0)
    
    sf = StyleFrame.read_excel('test.xlsx', read_style=True, use_openpyxl_styles=False)
    
    for name in sf.Names:
        if name.style.bold:
            print(name)
    

    With this Excel sheet:

    enter image description here

    Outputs

    bold
    bold