Home » excel » python – Appending multiple style on pandas excel worksheet

python – Appending multiple style on pandas excel worksheet

Posted by: admin April 23, 2020 Leave a comment

Questions:
  PandasWriter = pandas.ExcelWriter(self.sio, engine='xlsxwriter')
  workbook =  PandasWriter.book
  worksheet = PandasWriter.sheets['Sheet1']

  black_shade = workbook.add_format({'bg_color':'#f6f7f8'})
  bold = workboot.add_format({'bold': True})

  worksheet.set_columns('A:C',None, black_shade)
  worksheet.set_rows(20, None, bold)

Need to have A20 cell to have black_shade and be bold. How do i append two formats. Format set on columns gets replaced by format on rows.

How to&Answers:

One idea (little hack) is create new style with both previous and then use Conditional Formatting where is possible set columns and rows together:

black_shade = workbook.add_format({'bg_color':'#f6f7f8'})
bold = workbook.add_format({'bold': True})
black_shade_bold = workbook.add_format({'bold': True, 'bg_color':'#f6f7f8'})

worksheet.set_columns('A:C',None, black_shade)
worksheet.set_rows(20, None, bold)

worksheet.conditional_format('A20:A20', {'type': 'no_errors',
                                         'format': black_shade_bold})