excel 如何通过openpyxl清除整个工作簿的条件格式

t8e9dugd  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(107)

我写了一些代码来做条件格式。然而,每当我运行代码时,单元格都是用相同的条件格式编写的。有没有办法清除现有的并编写新的。
我查过官方文档https://openpyxl.readthedocs.io/en/stable/formatting.html,里面没有明确的功能

pu3pd22g

pu3pd22g1#

如果您只想清除特定工作表的格式,则只需创建一个新列表:

from openpyxl.formatting.formatting import ConditionalFormattingList
ws.conditional_formatting = ConditionalFormattingList()

字符串

20jt8wwn

20jt8wwn2#

如果您只想删除特定单元格/cell_range中的条件格式,请按照以下步骤删除条件格式。

wb = load_workbook("excel_file_path.xlsx")
ws = wb["sheet1"]
red_color = 'ffc7ce'
red_color_font = '9c0103'
red_font = styles.Font(size=14, bold=True, color=red_color_font)
red_fill = styles.PatternFill(start_color=red_color, end_color=red_color, 
            fill_type='solid')
# to get all cf_rules 
cf_rl = list(ws.conditional_formatting._cf_rules)

#to get specific cell from list of formatting
l1 = [i for i in cf_rl if i.sqref == "F51"]

#to delete conditional formatting from specific cell
del ws.conditional_formatting._cf_rules[l1[0]]

wb.save("test.xlsx")

字符串

相关问题