from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font
from openpyxl.styles.fills import FILL_SOLID
from openpyxl.styles.colors import BLACK
from openpyxl.utils import get_column_letter
wb = Workbook()
ws = wb.active
c = ws['A1']
fill = PatternFill(patternType=FILL_SOLID, fgColor=(rgb="0000FF")) # 这里使用的是 RGB格式
font = Font(color="FFBB00") # 这里使用RGB格式
ws.sheet_properties.tabColor = BLACK
# 填充一个单元格
c.value = "this is new value"
c.font = font
c.fill = fill
# 填充一行
cells = ws.row_dimensions[1]
row.font = font
row.fill = fill
# 填充指定长度的行
start_chr = "A"
end_chr = get_column_letter(5) # 这个方法可以解决AA列名问题
fill_cells = ws["%s1" % start_chr : "%s1" % end_chr]
for tmp_c in fill_cells[0]:
tmp_c.font = font
tmp_c.fill = font
wb.save(filename='test.xlsx')