0
0

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-07-27

Excelでセルの結合をしつつ、中央揃えにする。
例)D1:F1からD14:F15を結合し、中央揃え

============================================================
import openpyxl as px
from openpyxl.styles import Alignment  ← 中央揃えのためインポート

'Book2.xlsxをwb2に置き換えられるように、変数処理している
wb2 = px.load_workbook('Book2.xlsx')

#Book2.xlsxのSheet1をws2に置き換えられるように、変数処理している
ws2 = wb2['Sheet1']

#whileを使用し、14回まで数値を追加しつつループさせる。
i = 1
while i < 15:

  #[D+i:F+i]セルを結合する
  ws2.merge_cells('D'+ str(i) + ':F' + str(i))

  # D+iのセルに[中央揃え]を設定
  ws2['D' + str(i)].alignment = Alignment(horizontal='center')

  i += 1

#ブックを保存する
wb2.save(filename = 'Book2.xlsx')

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?