如何使用 openpyxl 读取工作表的标签颜色
How to read the tab color of a worksheet with openpyxl
我一直在为 python 3.8.3 使用 openpyxl 模块编写脚本。在我的脚本中,我使用了一个包含多个 sheet 的预先存在的 excel 工作簿。这个 sheet 来自另一个人,他们希望我只 运行 带有特定颜色的选项卡上的脚本。我一直试图通过使用 sheet_properties 中的 tabColor 来识别哪些作品 sheet 具有指定的颜色,但这对我不起作用。任何帮助表示赞赏!
这里
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor == "11217371":
common_compression(ws, storage_path)
对于遇到与我类似问题的任何人,我找到了我的解决方案。 sheet_properties.tabColor 使用的参数包含一个 'rgb' 参数和一个 'theme' 参数。在我的例子中,我处理的工作簿中的工作簿选项卡按主题颜色着色,而不是编写 excel sheet 的人选择的 rgb 值。在我的工作簿上,作者用默认主题颜色的 Accent 6 为他们的标签着色。所以对我来说,解决方案是寻找给定 sheet_properties.tabColor.theme 的索引等于 6,因为从零开始,我正在搜索的颜色是 [提供的列表中的第 9 个索引。 =22=].
这是我解决上述问题的工作条件:
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor.theme == 6:
common_compression(ws, storage_path)
这是 excel 中讨论的 Accents 列表的屏幕截图:
Example Accent listing from excel
我一直在为 python 3.8.3 使用 openpyxl 模块编写脚本。在我的脚本中,我使用了一个包含多个 sheet 的预先存在的 excel 工作簿。这个 sheet 来自另一个人,他们希望我只 运行 带有特定颜色的选项卡上的脚本。我一直试图通过使用 sheet_properties 中的 tabColor 来识别哪些作品 sheet 具有指定的颜色,但这对我不起作用。任何帮助表示赞赏! 这里
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor == "11217371":
common_compression(ws, storage_path)
对于遇到与我类似问题的任何人,我找到了我的解决方案。 sheet_properties.tabColor 使用的参数包含一个 'rgb' 参数和一个 'theme' 参数。在我的例子中,我处理的工作簿中的工作簿选项卡按主题颜色着色,而不是编写 excel sheet 的人选择的 rgb 值。在我的工作簿上,作者用默认主题颜色的 Accent 6 为他们的标签着色。所以对我来说,解决方案是寻找给定 sheet_properties.tabColor.theme 的索引等于 6,因为从零开始,我正在搜索的颜色是 [提供的列表中的第 9 个索引。 =22=].
这是我解决上述问题的工作条件:
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor.theme == 6:
common_compression(ws, storage_path)
这是 excel 中讨论的 Accents 列表的屏幕截图: Example Accent listing from excel