styles.fills【塗りつぶし】モジュール

メモ ( 概要 塗りつぶし (パターン)の指定方法 塗りつぶし (グラデーション)の指定方法 ) 定数
クラス:Fill【塗りつぶし (基底)】 GradientFill【塗りつぶし (パターン)】 PatternFill【塗りつぶし (グラデーション)】 Stop【グラデーション停止色】 StopList【グラデーション停止色リスト】

メモ

概要

塗りつぶし (パターン)の指定方法

  • PatternFill【塗りつぶし (パターン)】の作成
    • patternType・fill_type【塗りつぶしパターンの種類】と下記の色を指定し作成 (色を省略すると白黒で作成)
    • 塗りつぶし (なし):色指定なし
    • 塗りつぶし (単色):fgcolor・start_color【パターンの色】を指定
    • その他:fgcolor・start_color【パターンの色】bgcolor・end_color【背景色】を指定
  • 単一セルの指定
  • 複数セルの指定
    • 一括で指定できない為、該当セルに上記の単一セルの処理
  • 結合セルの指定
    • 左上セルに対して上記の単一セルの処理

塗りつぶし (グラデーション)の指定方法


定数

PatternFill【塗りつぶし (パターン)】クラス 参照

定数備考
塗りつぶしパターンの種類
patternTypefill_type
説明 パターンの色
fgcolor
start_color
背景色
bgcolor
end_color
FILL_NONE'none'塗りつぶし (なし)
FILL_SOLID'solid'塗りつぶし (単色)( 条件付き書式 )
FILL_PATTERN_DARKDOWN'darkDown'右下がり斜線 縞
FILL_PATTERN_DARKGRAY'darkGray'75% 灰色
FILL_PATTERN_DARKGRID'darkGrid'左下がり斜線 格子
FILL_PATTERN_DARKHORIZONTAL'darkHorizontal'横 縞
FILL_PATTERN_DARKTRELLIS'darkTrellis'極太線 左下がり斜線 格子
FILL_PATTERN_DARKUP'darkUp'左下がり斜線 縞
FILL_PATTERN_DARKVERTICAL'darkVertical'縦 縞
FILL_PATTERN_GRAY0625'gray0625'6.25% 灰色
FILL_PATTERN_GRAY125'gray125'12.5% 灰色
FILL_PATTERN_LIGHTDOWN'lightDown'実線 右下がり斜線 縞
FILL_PATTERN_LIGHTGRAY'lightGray'25% 灰色
FILL_PATTERN_LIGHTGRID'lightGrid'実線 横 格子
FILL_PATTERN_LIGHTHORIZONTAL'lightHorizontal'実線 横 縞
FILL_PATTERN_LIGHTTRELLIS'lightTrellis'実線 左下がり斜線 格子
FILL_PATTERN_LIGHTUP'lightUp'実線 左下がり斜線 縞
FILL_PATTERN_LIGHTVERTICAL'lightVertical'実線 縦 縞
FILL_PATTERN_MEDIUMGRAY'mediumGray'50% 灰色

※ 条件付き書式で使用する場合に指定
(詳細はformatting.rule【書式ルール】の注意事項 参照))


Fill【塗りつぶし (基底)】クラス

クラス定義

クラス備考
親:openpyxl.descriptors.serialisable.Serialisable
インポート:from openpyxl.styles import Fill (省略形)
class Fill( ) 塗りつぶし (基底)

プロパティ

プロパティ備考
tagnamestrタグ名 ('fill')

メソッド

クラスメソッド備考
@classmethod from_tree(
    el)

el

GradientFill【塗りつぶし (グラデーション)】クラス

クラス定義

クラス備考
親:openpyxl.styles.fills.Fill
インポート:from openpyxl.styles import GradientFill (省略形)
class GradientFill(
    type='linear',
    degree=0,
    left=0,
    right=0,
    top=0,
    bottom=0,
    stop=())
塗りつぶし (グラデーション)
type ( 'linear' | 'path' )グラデーション タイプ
degree (float)グラデーション角度
left (float)左辺からのグラデーション範囲 (0:左辺 ~ 1:右辺)
right (float)右辺からのグラデーション範囲 (0:左辺 ~ 1:右辺)
top (float)上辺からのグラデーション範囲 (0:上辺 ~ 1:下辺)
bottom (float)下辺からのグラデーション範囲 (0:上辺 ~ 1:下辺)
stop (リスト形式)グラデーション停止色リスト
※指定引数:type (グラデーション タイプ)stop (グラデーション停止色リスト)と下記
'linear'【線形グラデーション】:degree (グラデーション角度)を指定
'path'【範囲指定グラデーション】:degree (グラデーション角度)以外を指定

プロパティ

プロパティ備考
bottomfloat下辺からのグラデーション範囲 (0:上辺 ~ 1:下辺)
degreefloatグラデーション角度 (時計回り)

0:左から右
90:上から下
180:右から左
270:下から上
fill_typestrtype【グラデーション タイプ】のエイリアス
leftfloat左辺からのグラデーション範囲 (0:左辺 ~ 1:右辺)
rightfloat右辺からのグラデーション範囲 (0:左辺 ~ 1:右辺)
stopStopListグラデーション停止色リスト
tagnamestrタグ名 ("gradientFill")
topfloat上辺からのグラデーション範囲 (0:上辺 ~ 1:下辺)
type'linear'
'path'
グラデーション タイプ
'linear':線形グラデーション
'path':範囲指定グラデーション

メソッド

メソッド備考
to_tree(
    tagname=None,
    namespace=None,
    idx=None)

tagname
namespace
idx

PatternFill【塗りつぶし (パターン)】クラス

クラス定義

クラス
親:openpyxl.styles.fills.Fill
インポート:from openpyxl.styles import PatternFill (省略形)
class PatternFill(
    patternType=None,
    fgColor=Color(),
    bgColor=Color(),
    fill_type=None,
    start_color=None,
    end_color=None)
塗りつぶし (パターン)
patternType (str)パターンの種類 (fill_type)〔FILL_~【塗りつぶしパターンの種類】定数〕
fgColor (Color | str)パターンの色 (start_color / 省略:自動)
bgColor (Color | str)背景色 (end_color / 省略:色なし)
fill_type (str)パターンの種類 (優先)〔FILL_~【塗りつぶしパターンの種類】定数〕
start_color (Color | str)パターンの色 (優先)
end_color (Color | str)背景色 (優先)

プロパティ

プロパティ備考
bgColorColor背景色
end_colorColorbgColor【背景色】のエイリアス
fgColorColorパターンの色
fill_typestrpatternType【パターンの種類】のエイリアス
patternTypestr パターンの種類
個別の色指定は FILL_~【塗りつぶしパターンの種類】定数 参照
start_colorColorfgColor【パターンの色】のエイリアス
tagnamestrタグ名 ("patternFill")

メソッド

メソッド備考
to_tree(
    tagname=None,
    idx=None)

tagname
idx

Stop【グラデーション停止色】クラス

クラス定義

クラス
親:openpyxl.descriptors.serialisable.Serialisable
class Stop(
    color,
    position)

color
position

プロパティ

プロパティ備考
colorColorグラデーション停止色リスト
positionfloat
tagnamestrタグ名 ('stop')

StopList【グラデーション停止色リスト】クラス

クラス定義

クラス
親:openpyxl.descriptors.sequence.Sequence
class StopList(
    name=None,
    **kw)

name
kw

プロパティ

プロパティ備考
expected_type

塗りつぶし (パターン)の指定方法

from openpyxl import Workbook
from openpyxl.styles import PatternFill, fills
from openpyxl.styles import Alignment
from openpyxl.utils import get_column_letter

# ワークブック作成
WORKBOOK = 'sample.xlsx'
wb = Workbook()
ws = wb.active

# 色指定
fgcolor = '000000FF'
bgcolor = '0000FF00'

# 塗りつぶし (灰色)
fill_gray = [
    (fills.FILL_SOLID, '塗りつぶし (単色)'),
    (fills.FILL_PATTERN_DARKGRAY, '75% 灰色'),
    (fills.FILL_PATTERN_MEDIUMGRAY, '50% 灰色'),
    (fills.FILL_PATTERN_LIGHTGRAY, '25% 灰色'),
    (fills.FILL_PATTERN_GRAY125, '12.5% 灰色'),
    (fills.FILL_PATTERN_GRAY0625, '6.25% 灰色'),
    (fills.FILL_NONE, '塗りつぶし (なし)'),
]
row = 2
col = 2
ws.row_dimensions[row + 1].height = 30
ws.row_dimensions[row + 2].height = 30
for pattern, memo in fill_gray:
    cell = ws.cell(row, col)
    cell.alignment = Alignment(wrap_text=True)
    cell.value = pattern + '\n' + memo
    fill1 = PatternFill(pattern)
    if pattern == fills.FILL_NONE:
        fill2 = PatternFill(pattern)
    elif pattern == fills.FILL_SOLID:
        fill2 = PatternFill(pattern, fgcolor)
    else:
        fill2 = PatternFill(pattern, fgcolor, bgcolor)
    ws.cell(row + 1, col).fill = fill1
    ws.cell(row + 2, col).fill = fill2
    col += 1

# 塗りつぶし (DARK)
fill_dark = [
    (fills.FILL_PATTERN_DARKHORIZONTAL, '横 縞'),
    (fills.FILL_PATTERN_DARKVERTICAL, '縦 縞'),
    (fills.FILL_PATTERN_DARKDOWN, '右下がり斜線 縞'),
    (fills.FILL_PATTERN_DARKUP, '左下がり斜線 縞'),
    (fills.FILL_PATTERN_DARKGRID, '左下がり斜線 格子'),
    (fills.FILL_PATTERN_DARKTRELLIS, '極太線 左下がり斜線 格子'),
]
row += 4
col = 2
ws.row_dimensions[row + 1].height = 30
ws.row_dimensions[row + 2].height = 30
for pattern, memo in fill_dark:
    cell = ws.cell(row, col)
    cell.alignment = Alignment(wrap_text=True)
    cell.value = pattern + '\n' + memo
    fill1 = PatternFill(pattern)
    fill2 = PatternFill(pattern, fgcolor, bgcolor)
    ws.cell(row + 1, col).fill = fill1
    ws.cell(row + 2, col).fill = fill2
    col += 1

# 塗りつぶし (LIGHT)
fill_light = [
    (fills.FILL_PATTERN_LIGHTHORIZONTAL, '実線 横 縞'),
    (fills.FILL_PATTERN_LIGHTVERTICAL, '実線 縦 縞'),
    (fills.FILL_PATTERN_LIGHTDOWN, '実線 右下がり斜線 縞'),
    (fills.FILL_PATTERN_LIGHTUP, '実線 左下がり斜線 縞'),
    (fills.FILL_PATTERN_LIGHTGRID, '実線 横 格子'),
    (fills.FILL_PATTERN_LIGHTTRELLIS, '実線 左下がり斜線 格子'),
]
row += 4
col = 2
ws.row_dimensions[row + 1].height = 30
ws.row_dimensions[row + 2].height = 30
for pattern, memo in fill_light:
    cell = ws.cell(row, col)
    cell.alignment = Alignment(wrap_text=True)
    cell.value = pattern + '\n' + memo
    fill1 = PatternFill(pattern)
    fill2 = PatternFill(pattern, fgcolor, bgcolor)
    ws.cell(row + 1, col).fill = fill1
    ws.cell(row + 2, col).fill = fill2
    col += 1

# 列幅
for col in range(2, 9):
    letter = get_column_letter(col)
    ws.column_dimensions[letter].width = 15

# 複数セルの指定
row += 4
ws.cell(row, 2).value = '複数セル'
fill = PatternFill(fills.FILL_PATTERN_LIGHTVERTICAL)
# 1行
cells = ws.iter_rows(row, row, 5, 7)
for row_inf in cells:
    for cell in row_inf:
        cell.fill = fill
# 1列
cells = ws.iter_rows(row + 2, row + 4, 3, 3)
for row_inf in cells:
    for cell in row_inf:
        cell.fill = fill
# 複数行・複数列
cells = ws.iter_rows(row + 2, row + 4, 5, 7)
for row_inf in cells:
    for cell in row_inf:
        cell.fill = fill

# 結合セルの指定 (左上のセル)
row += 6
ws.cell(row, 2).value = '結合セル'
fill = PatternFill(fills.FILL_PATTERN_LIGHTHORIZONTAL)
ws.merge_cells(
    None,
    row, 3, row + 2, 5,
)
ws.cell(row, 3).fill = fill

# ワークブック保存
wb.save(WORKBOOK)

塗りつぶし (パターン)のサンプル画像


塗りつぶし (グラデーション)の指定方法

from openpyxl import Workbook
from openpyxl.styles import GradientFill

# ワークブック作成
WORKBOOK = 'sample.xlsx'
wb = Workbook()
ws = wb.active

# 設定値
color1 = '00FFFFFF'
color2 = '004F81BD'
color3 = '00FF0000'
HEIGHT = 35
ws.column_dimensions['A'].width = 2
ws.column_dimensions['B'].width = 10
ws.column_dimensions['C'].width = 6
ws.column_dimensions['D'].width = 6
ws.column_dimensions['E'].width = 6
ws.column_dimensions['F'].width = 6
ws.column_dimensions['G'].width = 6

# linear
ws['B2'] = 'linear'
ws['C2'].fill = GradientFill(
    'linear',
    0, stop=(color1, color2))
ws['D2'].fill = GradientFill(
    'linear',
    0, stop=(color1, color2, color3))
ws['E2'].fill = GradientFill(
    'linear',
    90, stop=(color1, color2))
ws['F2'].fill = GradientFill(
    'linear',
    90, stop=(color1, color2, color3))
ws.row_dimensions[2].height = HEIGHT

# path
ws['B4'] = 'path'
ws['C4'].fill = GradientFill(
    'path',
    left=0.5, right=0.5, top=0.5, bottom=0.5,
    stop=(color1, color2))
ws['D4'].fill = GradientFill(
    'path',
    left=0.5, right=0.5, top=0.5, bottom=0.5,
    stop=(color1, color2, color3))
ws['E4'].fill = GradientFill(
    'path',
    left=0.3, right=0.7, top=0.3, bottom=0.7,
    stop=(color1, color2))
ws['F4'].fill = GradientFill(
    'path',
    left=0.3, right=0.7, top=0.3, bottom=0.7,
    stop=(color1, color2, color3))
ws.row_dimensions[4].height = HEIGHT

# 複数セルの指定
ws['B6'] = '複数セル'
for i in range(6, 11):
    ws.row_dimensions[i].height = HEIGHT
fill = GradientFill(
    'linear',
    45, stop=(color1, color2, color3))
# 1行
cells = ws['E6:G6']
print(cells)
# 出力:
# ((<Cell 'Sheet'.E6>, <Cell 'Sheet'.F6>, <Cell 'Sheet'.G6>),)
for row in cells:
    for cell in row:
        cell.fill = fill
# 1列
cells = ws['C8:C10']
print(cells)
# 出力:
# ((<Cell 'Sheet'.C8>,), (<Cell 'Sheet'.C9>,), (<Cell 'Sheet'.C10>,))
for row in cells:
    for cell in row:
        cell.fill = fill
# 複数行・複数列
cells = ws['E8:G10']
print(cells)
# 出力:
# ((<Cell 'Sheet'.E8>, <Cell 'Sheet'.F8>, <Cell 'Sheet'.G8>),
#  (<Cell 'Sheet'.E9>, <Cell 'Sheet'.F9>, <Cell 'Sheet'.G9>),
#  (<Cell 'Sheet'.E10>, <Cell 'Sheet'.F10>, <Cell 'Sheet'.G10>))
for row in cells:
    for cell in row:
        cell.fill = fill

# 結合セルの指定 (左上のセル)
ws['B12'] = '結合セル'
for i in range(12, 15):
    ws.row_dimensions[i].height = HEIGHT
fill = GradientFill(
    'linear',
    135, stop=(color1, color2, color3))
ws.merge_cells('C12:E14')
ws['C12'].fill = fill

# ワークブック保存
wb.save(WORKBOOK)

塗りつぶし (グラデーション)のサンプル画像


塗りつぶし (グラデーション)の指定方法 (Excel 既定)

from openpyxl import Workbook
from openpyxl.styles import GradientFill
from openpyxl.styles import PatternFill
from openpyxl.styles import fills

# ワークブック作成
WORKBOOK = 'sample.xlsx'
wb = Workbook()
ws = wb.active

# 設定値
color1 = '00FFFFFF'
color2 = '004F81BD'
colorA = '0000FF00'
colorB = '000000FF'
HEIGHT = 35
ws.column_dimensions['A'].width = 2
ws.column_dimensions['B'].width = 12
ws.column_dimensions['C'].width = 6
ws.column_dimensions['D'].width = 6
ws.column_dimensions['E'].width = 2
ws.column_dimensions['F'].width = 6
ws.column_dimensions['G'].width = 6

# Excelの規定値 [グラデーションの種類] - [バリエーション]
ws['B2'] = '[グラデーションの種類] - [バリエーション]'
ws['B3'] = '色 1'
ws['B4'] = '色 2'
ws['C3'].fill = PatternFill(fills.FILL_SOLID, color1)
ws['C4'].fill = PatternFill(fills.FILL_SOLID, color2)
ws['F3'].fill = PatternFill(fills.FILL_SOLID, colorA)
ws['F4'].fill = PatternFill(fills.FILL_SOLID, colorB)

ws['B6'] = 'linear【線形グラデーション】'
# 横
ws['B7'] = '横'
ws['C7'].fill = GradientFill(
    'linear',
    90, stop=(color1, color2))
ws['D7'].fill = GradientFill(
    'linear',
    270, stop=(color1, color2))
ws['C8'].fill = GradientFill(
    'linear',
    90, stop=(color1, color2, color1))
ws.row_dimensions[7].height = HEIGHT
ws.row_dimensions[8].height = HEIGHT
ws['F7'].fill = GradientFill(
    'linear',
    90, stop=(colorA, colorB))
ws['G7'].fill = GradientFill(
    'linear',
    270, stop=(colorA, colorB))
ws['F8'].fill = GradientFill(
    'linear',
    90, stop=(colorA, colorB, colorA))

# 縦
ws['B10'] = '縦'
ws['C10'].fill = GradientFill(
    'linear',
    0, stop=(color1, color2))
ws['D10'].fill = GradientFill(
    'linear',
    180, stop=(color1, color2))
ws['C11'].fill = GradientFill(
    'linear',
    0, stop=(color1, color2, color1))
ws.row_dimensions[10].height = HEIGHT
ws.row_dimensions[11].height = HEIGHT
ws['F10'].fill = GradientFill(
    'linear',
    0, stop=(colorA, colorB))
ws['G10'].fill = GradientFill(
    'linear',
    180, stop=(colorA, colorB))
ws['F11'].fill = GradientFill(
    'linear',
    0, stop=(colorA, colorB, colorA))

# 右上対角線
ws['B13'] = '右上対角線'
ws['C13'].fill = GradientFill(
    'linear',
    45, stop=(color1, color2))
ws['D13'].fill = GradientFill(
    'linear',
    225, stop=(color1, color2))
ws['C14'].fill = GradientFill(
    'linear',
    45, stop=(color1, color2, color1))
ws.row_dimensions[13].height = HEIGHT
ws.row_dimensions[14].height = HEIGHT
ws['F13'].fill = GradientFill(
    'linear',
    45, stop=(colorA, colorB))
ws['G13'].fill = GradientFill(
    'linear',
    225, stop=(colorA, colorB))
ws['F14'].fill = GradientFill(
    'linear',
    45, stop=(colorA, colorB, colorA))

# 右下対角線
ws['B16'] = '右下対角線'
ws['C16'].fill = GradientFill(
    'linear',
    135, stop=(color1, color2))
ws['D16'].fill = GradientFill(
    'linear',
    315, stop=(color1, color2))
ws['C17'].fill = GradientFill(
    'linear',
    135, stop=(color1, color2, color1))
ws.row_dimensions[16].height = HEIGHT
ws.row_dimensions[17].height = HEIGHT
ws['F16'].fill = GradientFill(
    'linear',
    135, stop=(colorA, colorB))
ws['G16'].fill = GradientFill(
    'linear',
    315, stop=(colorA, colorB))
ws['F17'].fill = GradientFill(
    'linear',
    135, stop=(colorA, colorB, colorA))

ws['B19'] = 'path【範囲指定グラデーション】'
# 角から
ws['B20'] = '角から'
ws['C20'].fill = GradientFill(
    'path',
    left=0.0, right=0.0, top=0.0, bottom=0.0,
    stop=(color1, color2))
ws['D20'].fill = GradientFill(
    'path',
    left=1.0, right=1.0, top=0.0, bottom=0.0,
    stop=(color1, color2))
ws['C21'].fill = GradientFill(
    'path',
    left=0.0, right=0.0, top=1.0, bottom=1.0,
    stop=(color1, color2))
ws['D21'].fill = GradientFill(
    'path',
    left=1.0, right=1.0, top=1.0, bottom=1.0,
    stop=(color1, color2))
ws.row_dimensions[20].height = HEIGHT
ws.row_dimensions[21].height = HEIGHT
ws['F20'].fill = GradientFill(
    'path',
    left=0.0, right=0.0, top=0.0, bottom=0.0,
    stop=(colorA, colorB))
ws['G20'].fill = GradientFill(
    'path',
    left=1.0, right=1.0, top=0.0, bottom=0.0,
    stop=(colorA, colorB))
ws['F21'].fill = GradientFill(
    'path',
    left=0.0, right=0.0, top=1.0, bottom=1.0,
    stop=(colorA, colorB))
ws['G21'].fill = GradientFill(
    'path',
    left=1.0, right=1.0, top=1.0, bottom=1.0,
    stop=(colorA, colorB))

# 中央から
ws['B23'] = '中央から'
ws['C23'].fill = GradientFill(
    'path',
    left=0.5, right=0.5, top=0.5, bottom=0.5,
    stop=(color1, color2))
ws.row_dimensions[23].height = HEIGHT
ws['F23'].fill = GradientFill(
    'path',
    left=0.5, right=0.5, top=0.5, bottom=0.5,
    stop=(colorA, colorB))

# ワークブック保存
wb.save(WORKBOOK)

塗りつぶし (グラデーション) Excel 既定 のサンプル画像