I am writing multiple sheets to an excel workbook with some formatting. In general everything works, just the formatting is a little off. The formatting works exactly as I want on the first sheet, but I am looping through multiple data sets and writing to a sheet for each one and the subsequent sheets have the formatting a little differently.
Basically what I tried to setup was header is centered everything else is left aligned. On the first sheet its as expected, but subsequent sheet header and everything else is left aligned except for my rows that are apply_style_by_indexes
which are now centered. So its like the apply_header_style
and apply_style_by_indexes
swapped or something.
Im guessing its something to do with how im defining the different styles and applying them.
data_dict = {args.source_env: {}, args.target_env: {}, "compare": {}}
for app in uniq_apps:
# set some vars
#setup lists for pandas
pd_app_inst = []
pd_platforms = []
pd_src_env = []
pd_src_rel_type = []
pd_src_version = []
pd_tgt_env = []
pd_tgt_rel_type = []
pd_tgt_version = []
pd_param = []
pd_src_value = []
pd_tgt_value = []
pd_columns = ['AppInst','Platform','SrcEnv','SrcRelType','SrcVersion','TgtEnv','TgtRelType','TgtVersion','Param','SrcValue','TgtValue']
write=False
logger.info(f'Comparing config for {app}')
if data_dict[args.source_env][app]["config"] and data_dict[args.target_env][app]["config"]:
src_cfg = data_dict[args.source_env][app]["config"]
tgt_cfg = data_dict[args.target_env][app]["config"]
write=True
# add some data to data_dict
else:
logger.info(f"The app instance {app} does not exist in one of the environments, nothing to compare so skipping")
continue
params = list(data_dict["compare"][app])
params.sort()
for param in params:
pd_app_inst.append(app)
pd_platforms.append(data_dict[args.source_env][app]["platform"])
pd_src_env.append(args.source_env)
pd_src_rel_type.append(data_dict[args.source_env][app]["rel_type"])
pd_src_version.append(data_dict[args.source_env][app]["version"])
pd_tgt_env.append(args.target_env)
pd_tgt_rel_type.append(data_dict[args.target_env][app]["rel_type"])
pd_tgt_version.append(data_dict[args.target_env][app]["version"])
pd_param.append(param)
pd_src_value.append(data_dict["compare"][app][param][0])
pd_tgt_value.append(data_dict["compare"][app][param][1])
df = pd.DataFrame(list(zip(pd_app_inst,pd_platforms,pd_src_env,pd_src_rel_type,pd_src_version,pd_tgt_env,pd_tgt_rel_type,pd_tgt_version,pd_param,pd_src_value,pd_tgt_value)),columns=pd_columns)
sf = StyleFrame(df)
default_style = Styler(font_size=10, horizontal_alignment='left', wrap_text=False)
sf = StyleFrame(df, styler_obj=default_style)
header_style = Styler(bold=True, font_size=14, horizontal_alignment='center')
sf.apply_headers_style(styler_obj=header_style)
sf.apply_style_by_indexes(sf[sf['SrcValue'] != sf['TgtValue']], styler_obj=Styler(bg_color='yellow',font_size=10,horizontal_alignment='left',bold=True))
sf.set_column_width(columns=['AppInst','Platform','SrcEnv','TgtEnv'], width=15)
sf.set_column_width(columns=['SrcRelType','SrcVersion','TgtRelType','TgtVersion'], width=25)
sf.set_column_width(columns=['Param'], width=65)
sf.set_column_width(columns=['SrcValue','TgtValue'], width=80)
sf.set_row_height(rows=sf.row_indexes, height=15)
with pd.ExcelWriter(excel_file,mode='a') as writer:
sf.to_excel(writer, sheet_name=app, freeze_panes=(1,0), index=False)
logger.info(f'Comparison written to output file for {app}')