pythonpandasstyleframe

Using Styleframe to pull styles of individual cells from Excel


I'm trying to write a script that merges two excel files together. One has been has been hand processed and has a bunch custom formatting done to it, and the other is an auto-generated file. Doing the merge in pandas is simple enough, but preserving the formatting is proving troublesome. I found the styleframe library, which seems like it should simplify what I'm trying to do, as it can import style info in addition to the raw data. However, I'm having problems actually implementing the code.

My questions is this: how can I pull style information from each individual cell in the excel and then apply that to my merged dataframe? Note that the data is not formatted consistently across columns or rows, so I don't think I can apply styles in this manner. Here's the relevant portion of my code:

#iterate thorough all cells of merged dataframe
for rownum, row in output_df.iterrows():
    for  column, value in row.iteritems(): 
        filename = row['File Name']

        cur_style = orig_excel.loc[orig_excel['File Name'] == filename, column][0].style #pulls the style of relevant cell in the original excel document
        target_style = output_df.loc[output_df['File Name'] == filename, column][0].style #style of the cell in the merged dataframe
        target_style = cur_style #set style in current output_df cell to match original excel file style

This code runs (slowly) but it doesn't seem to actually apply any styling to the output styleframe

Looking through the documentation, I don't really see a method for applying styles at an individual styleframe container level--everything is geared towards doing it as a row or column. It also seems like you need to use a styler object to set the style.


Solution

  • Figured it out. I rejiggered my dataframe so that I could just us a .at instead of a .loc lookup. This, coupled with the apply_style_by_indexes method got me where I needed to be:

    for index, row in orig_excel.iterrows():
        for  column, value in row.iteritems(): 
            index_num = output_df.index.get_loc(index)
    
            #Pull style to copy to new df
            cur_style = orig_excel.at[index, column].style 
          
            #Apply original style to new df
            output_df.apply_style_by_indexes(output_df.index[index_num], 
                                             cur_style, 
                                             cols_to_style = column)