Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring a tab in openpyxl

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)?

like image 991
aperkins Avatar asked Mar 27 '13 19:03

aperkins


2 Answers

You can color the tabs with openpyxl by using RRGGBB color code for sheet_properties.tabColor property:

from openpyxl import Workbook

wb = Workbook()
ws = wb.create_sheet('My_Color_Title')
ws.sheet_properties.tabColor = 'FFFF00'

wb.save('My_book_with_Yellow_Tab.xlsx')

enter image description here

like image 160
ISQ Avatar answered Sep 21 '22 08:09

ISQ


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()

Coloured tabs in Excel worksheet using Python

like image 45
jmcnamara Avatar answered Sep 20 '22 08:09

jmcnamara