![](/img/trans.png)
[英]How to detect protected worksheet in excel workbook using Python xlwings?
[英]How to get/set the colour of the worksheet tab using xlwings in Python?
以下示例來自xlsxwriter
的文檔:
import xlsxwriter
workbook = xlsxwriter.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 colors
worksheet1.set_tab_color('red')
worksheet2.set_tab_color('green')
worksheet3.set_tab_color('#FF9900') # Orange
# worksheet4 will have the default color.
workbook.close()
是否可以在 xlwings 中做同樣的事情? 我在 Sheet 類的代碼中看不到任何與顏色相關的內容。
如果您知道,您可以使用 xlwings api 並使用 Excel 顏色編號設置顏色;
例如
255紅色
65280 綠色
16711680 藍色
或 RBG 顏色代碼
或使用顏色索引
import xlwings as xw
# Set sheet tab to Blue
wb = xw.Book('tab_colors.xlsx')
# Either
wb.sheets['sheet1'].api.Tab.Color = 16711680
wb.sheets['sheet2'].api.Tab.Color = '&HFF0000'
wb.sheets['sheet3'].api.Tab.ColorIndex = 5
在 Windows 上,可以使用 pywin32 api 獲取和設置選項卡的顏色。 請注意,它使您的代碼平台特定,例如,它不適用於 Mac。
import xlwings as xw
from xlwings.utils import rgb_to_int, int_to_rgb
# Define RGB codes
green = (226, 239, 218)
grey = (242, 242, 242)
red = (252, 228, 214)
# Connect to the Excel file
wb = xw.Book(EXCEL_FILENAME)
for sht in wb.sheets:
# Retrieve the tab colour
print("Sheet name {}, Tab color int: {}, Tab color RGB: {}".format(sht.name, sht.api.Tab.Color, int_to_rgb(sht.api.Tab.Color)))
# Set the tab colour to these existing tabs
wb.sheets["Test Green"].api.Tab.Color = rgb_to_int(green)
wb.sheets["Test Grey"].api.Tab.Color = rgb_to_int(grey)
wb.sheets["Test Red"].api.Tab.Color = rgb_to_int(red)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.