Search code examples
pandasexceldataframepivot-table

The data fetch from pandas pivot table should be in normal font


I want to write a data frame to Excel. I have done it using pandas pivot_table. I have passed the column names as indexes. I am getting the excel, but it's in bold letters, I only need the column names as bold the values need to be in normal font.

import pandas as pd

# Create a test df
df = pd.DataFrame(
    {'Name': ['ravi', 'Deepthy','ravi'],
                   'Ship': ['mount everes', 'mount everes', 'Oasis of the Seas'],  
                   'Tracking': ['TESTTRACK003', 'TESTTRACK008', 'TESTTRACK009'],
                    'Bag': ['123', '129', '129'],
    })

df=pd.pivot_table(df,values=["Name","Ship","Tracking","Bag"],
                  index=["Name","Ship","Tracking","Bag"])

def normal_font(_):
    return "font-weight: normal"

df.style.applymap_index(normal_font).to_excel("abcde.xlsx", engine="openpyxl",startrow=-1)

Solution

  • You can use applymap_index with CSS font-weight property.

    Try this :

    def normal_font(_):
        return "font-weight: normal"
    
    df.style.applymap_index(normal_font).to_excel("abcde.xlsx", engine="openpyxl",startrow=-1)
    

    # Output (in Excel)

    enter image description here