styles.fills【塗りつぶし】モジュール
メモ ( 概要 塗りつぶし (パターン)の指定方法 塗りつぶし (グラデーション)の指定方法 ) 定数 例
クラス:Fill【塗りつぶし (基底)】 GradientFill【塗りつぶし (パターン)】 PatternFill【塗りつぶし (グラデーション)】 Stop【グラデーション停止色】 StopList【グラデーション停止色リスト】
メモ
概要
- Cell【セル】の塗りつぶしに関連するモジュール
- Excelの [セルの書式設定]-[塗りつぶし]タブ に対応
- Excelの [セルの書式設定]-[塗りつぶし]タブ に対応
- モジュール内定義
- インポート
- 下記は、openpyxl.styles からインポート可
PatternFill【塗りつぶし (パターン)】クラス
GradientFill【塗りつぶし (グラデーション)】クラス
Fill【塗りつぶし (基底)】クラス
- 下記は、openpyxl.styles からインポート可
- 塗りつぶしの種類
- パターン:PatternFill【塗りつぶし (パターン)】
- グラデーション:GradientFill【塗りつぶし (グラデーション)】
- その他スタイルは、cell【セル】モジュール 参照 (個々の詳細は下記モジュール)
- styles.numbers【表示形式】 ・styles.alignment【配置】 ・styles.fonts【フォント】 ・styles.borders【罫線】 ・styles.protection【シート保護】
- テキスト色は、styles.fonts【フォント】参照
- 関連モジュール
塗りつぶし (パターン)の指定方法
〔 例 〕- PatternFill【塗りつぶし (パターン)】の作成
- patternType・fill_type【塗りつぶしパターンの種類】と下記の色を指定し作成 (色を省略すると白黒で作成)
- 塗りつぶし (なし):色指定なし
- 塗りつぶし (単色):fgcolor・start_color【パターンの色】を指定
- その他:fgcolor・start_color【パターンの色】と bgcolor・end_color【背景色】を指定
- 単一セルの指定
- Cell【セル】 の fill【塗りつぶし】プロパティ に PatternFill【塗りつぶし (パターン)】を指定
- 複数セルの指定
- 一括で指定できない為、該当セルに上記の単一セルの処理
- 結合セルの指定
- 左上セルに対して上記の単一セルの処理
塗りつぶし (グラデーション)の指定方法
〔 例 〕- GradientFill【塗りつぶし (グラデーション)】の作成
Excel 上の手動でできる設定に限定するには、 例:Excel 既定 参照- type【グラデーション タイプ】と stop【グラデーション停止色リスト】と下記を指定し作成
- 'linear'【線形グラデーション】:degree【グラデーション角度】を指定
- 'path'【範囲指定グラデーション】:各辺からのグラデーション範囲を指定 (degree【グラデーション角度】以外)
- 単一セルの指定
- Cell【セル】 の fill【塗りつぶし】プロパティ に GradientFill【塗りつぶし (グラデーション)】を指定
- 複数セルの指定
- 一括で指定できない為、該当セルに上記の単一セルの処理
- 結合セルの指定
- 左上セルに対して上記の単一セルの処理
外部リンク
- openpyxl (英語)
- ECMA-376:Office Open XML file formats (英語)
SpreadsheetML:Styles -- bgColor (Background Color)
- fgColor (Foreground Color)
- fill (Fill)
- fills (Fills)
- gradientFill (Gradient)
- patternFill (Pattern)
- stop (Gradient Stop)
定数
PatternFill【塗りつぶし (パターン)】クラス 参照
定数 | 値 | 備考 | ||
---|---|---|---|---|
塗りつぶしパターンの種類 patternType・fill_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( ) | 塗りつぶし (基底) |
プロパティ
プロパティ | 型 | 備考 |
---|---|---|
tagname | str | タグ名 ('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 (グラデーション角度)以外を指定 |
プロパティ
プロパティ | 型 | 備考 |
---|---|---|
bottom | float | 下辺からのグラデーション範囲 (0:上辺 ~ 1:下辺) |
degree | float | グラデーション角度 (時計回り) 例 0:左から右 90:上から下 180:右から左 270:下から上 |
fill_type | str | type【グラデーション タイプ】のエイリアス |
left | float | 左辺からのグラデーション範囲 (0:左辺 ~ 1:右辺) |
right | float | 右辺からのグラデーション範囲 (0:左辺 ~ 1:右辺) |
stop | StopList | グラデーション停止色リスト |
tagname | str | タグ名 ("gradientFill") |
top | float | 上辺からのグラデーション範囲 (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)背景色 (優先) |
プロパティ
プロパティ | 型 | 備考 |
---|---|---|
bgColor | Color | 背景色 |
end_color | Color | bgColor【背景色】のエイリアス |
fgColor | Color | パターンの色 |
fill_type | str | patternType【パターンの種類】のエイリアス |
patternType | str | パターンの種類 個別の色指定は FILL_~【塗りつぶしパターンの種類】定数 参照 |
start_color | Color | fgColor【パターンの色】のエイリアス |
tagname | str | タグ名 ("patternFill") |
メソッド
メソッド | 備考 |
---|---|
to_tree( tagname=None, idx=None) | tagname idx |
Stop【グラデーション停止色】クラス
クラス定義
クラス | |
---|---|
親:openpyxl.descriptors.serialisable.Serialisable | |
class Stop( color, position) | color position |
プロパティ
プロパティ | 型 | 備考 |
---|---|---|
color | Color | グラデーション停止色リスト |
position | float | |
tagname | str | タグ名 ('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)