Coloring a tab in openpyxl

openpyxl, python

Solution

You can set the tab color in a new Excel file using the XlsxWriter Python module. Here is an example:

from xlsxwriter.workbook import Workbook

workbook = Workbook('tab_colors.xlsx')

# Set up some worksheets.
worksheet1 = workbook.add_worksheet()
worksheet2 = workbook.add_worksheet()
worksheet3 = workbook.add_worksheet()
worksheet4 = workbook.add_worksheet()

# Set tab colours
worksheet1.set_tab_color('red')
worksheet2.set_tab_color('green')
worksheet3.set_tab_color('#FF9900')  # Orange

# worksheet4 will have the default colour.
workbook.close()

Problem

We have a situation where we want to color the tabs for the worksheets using openpyxl. Is there a way to do this within the library? Or, has anyone found a way to do this external to the library (i.e. by extension or something similar)?

Original source