Coloring a tab in openpyxl

这一生的挚爱 提交于 2020-02-02 00:28:04

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15667750/coloring-a-tab-in-openpyxl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!