openpyxl (2.4.8) 风格背景问题;总是黑色

use*_*960 4 python openpyxl

我最近将我的代码切换到 openpyxl 以利用更新工作簿的能力,但我在背景样式方面遇到了问题。我使用颜色和十六进制值尝试了以下代码的几种变体,我总是得到相同的结果,即黑色背景和粗体白色文本。提前致谢!

from openpyxl.styles import NamedStyle, PatternFill, Border, Side, 
Alignment, Protection, colors, Font

styl_hdg = NamedStyle(name="styl_hdg")

styl_hdg.font = Font(color=colors.WHITE, bold=True)
styl_hdg.alignment = Alignment(wrap_text=True)
styl_hdg.fill = PatternFill(bgColor=colors.DARKGREEN, fill_type="solid")

ws.cell(row=myrow, column=mycol).style = styl_hdg
Run Code Online (Sandbox Code Playgroud)

我也试过这个,因为有时我会得到并且已经定义了错误样式。当我将行添加到现有工作簿/工作表时。

ws.cell(row=myrow, column=mycol).font = Font(bold=True,color=colors.WHITE)
    ws.cell(row=myrow, column=mycol).fill = 
PatternFill(fill_type="solid",bgColor=colors.DARKGREEN)
Run Code Online (Sandbox Code Playgroud)

Cha*_*ark 5

来自 OOXML 规范:

此元素用于指定图案和纯色单元格填充的单元格填充信息。对于实体单元格填充(无图案),使用 fgColor。

所以你需要设置 fgColor