Writing a function that applies styles to a cell range, wondering if there's some sort of metaprogramming way to avoid having a big if-else block here. Like being able to get which attribute I want to set from a dictionary that maps from Type -> attribute.
def _apply_style_to_range(self, cell_range, style):
for row in self.worksheet[cell_range]:
for cell in row:
if isinstance(style, openpyxl.styles.Alignment):
self.worksheet[cell.coordinate].alignment = style
elif isinstance(style, openpyxl.styles.Border):
self.worksheet[cell.coordinate].border = style
elif isinstance(style, openpyxl.styles.PatternFill):
self.worksheet[cell.coordinate].fill = style
elif isinstance(style, openpyxl.styles.Font):
self.worksheet[cell.coordinate].font = style
elif style in Numbers.all_number_formats: # these are custom strings I came up with so no defining type
self.worksheet[cell.coordinate].number_format = style
else:
raise ValueError('unhandled style {} unable to be applied'.format(style))