openpyxl - 无法将字体添加到单元格 - 不是已知样式
openpyxl - can't add font to cell - is not known style
我正在测试尝试将背景颜色添加到任何以单词 ultrasound
作为值的单元格。我正在使用 openpyxl
到目前为止我有:
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill, colors
my_fill = PatternFill(fill_type=None,
start_color='FFFFFFFF',
end_color='FF000000')
wb = load_workbook('TestTemplate.xlsx')
ws = wb.active
for row in range(1,ws.max_row):
for col in range(1,ws.max_column):
c = ws.cell(row = row, column = col)
if c.value == "Ultrasound":
c.style = my_fill
wb.save("testfont.xlsx")
但是它在第 c.style = my_fill
行出错说:
Traceback (most recent call last):
File "execelpytesttim.py", line 16, in <module>
c.style = my_fill
File "/usr/local/lib/python3.8/dist-packages/openpyxl/styles/styleable.py", line 85, in __set__
raise ValueError("{0} is not a known style".format(value))
ValueError: <openpyxl.styles.fills.PatternFill object>
Parameters:
patternType=None, fgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='FFFFFFFF', indexed=None, auto=None, theme=None, tint=0.0, type='rgb', bgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='FF000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb' is not a known style
我尝试了不同的十六进制值,但仍然失败。如果我尝试将十六进制值更改为像 blue
这样的颜色值,它会给出一个错误,指出 rgb 值必须是十六进制。
要在单元格中获取背景颜色,我是否缺少某些东西?
将其作为答案重新发布:很高兴它对您有用
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill, colors
# changed fill_type from None to 'solid'
my_fill = PatternFill(fill_type='solid',
start_color='FFFFFFFF',
end_color='FF000000')
wb = load_workbook('TestTemplate.xlsx')
ws = wb.active
for row in range(1,ws.max_row):
for col in range(1,ws.max_column):
c = ws.cell(row = row, column = col)
if c.value == "Ultrasound":
# Changed c.style = my_fill to following
c.fill = my_fill
wb.save("testfont.xlsx")
我正在测试尝试将背景颜色添加到任何以单词 ultrasound
作为值的单元格。我正在使用 openpyxl
到目前为止我有:
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill, colors
my_fill = PatternFill(fill_type=None,
start_color='FFFFFFFF',
end_color='FF000000')
wb = load_workbook('TestTemplate.xlsx')
ws = wb.active
for row in range(1,ws.max_row):
for col in range(1,ws.max_column):
c = ws.cell(row = row, column = col)
if c.value == "Ultrasound":
c.style = my_fill
wb.save("testfont.xlsx")
但是它在第 c.style = my_fill
行出错说:
Traceback (most recent call last):
File "execelpytesttim.py", line 16, in <module>
c.style = my_fill
File "/usr/local/lib/python3.8/dist-packages/openpyxl/styles/styleable.py", line 85, in __set__
raise ValueError("{0} is not a known style".format(value))
ValueError: <openpyxl.styles.fills.PatternFill object>
Parameters:
patternType=None, fgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='FFFFFFFF', indexed=None, auto=None, theme=None, tint=0.0, type='rgb', bgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='FF000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb' is not a known style
我尝试了不同的十六进制值,但仍然失败。如果我尝试将十六进制值更改为像 blue
这样的颜色值,它会给出一个错误,指出 rgb 值必须是十六进制。
要在单元格中获取背景颜色,我是否缺少某些东西?
将其作为答案重新发布:很高兴它对您有用
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill, colors
# changed fill_type from None to 'solid'
my_fill = PatternFill(fill_type='solid',
start_color='FFFFFFFF',
end_color='FF000000')
wb = load_workbook('TestTemplate.xlsx')
ws = wb.active
for row in range(1,ws.max_row):
for col in range(1,ws.max_column):
c = ws.cell(row = row, column = col)
if c.value == "Ultrasound":
# Changed c.style = my_fill to following
c.fill = my_fill
wb.save("testfont.xlsx")