2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでExcelの加工 フィルタ設定、セル塗りつぶし、中央揃え

Last updated at Posted at 2024-03-05

仕事でPythonでExcelの加工を依頼されたため、
その時に使用したフィルタ設定、セル塗りつぶし、中央揃えをメモにまとめておきます。
どなたかの役に立つと嬉しいです。

import openpyx|
from openpyxl.styles import PatternFill
from openpyxl.styles import Alignment

wb = openpyxl.load_workbook('対象ファイルパス')

ws = wb['対象シート名']

#A〜D列をフィルタ設定
ws.auto_filter.ref = 'A1:D1'

grayfill = PatternFill(patternType='solid' , fgColor='d3d3d3')
for row in ws['D2:D5']:
  for cell in row:
    #D列のセルを中央揃えに設定
    cell.alignment = Alignment(horizontal='general")
    #D列の2行目〜5行目を灰色塗りつぶし
    cell.fill = grayfill
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?