0
0

【Python】Excel結合と中央揃え

Posted at

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

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

wb2 = px.load_workbook('Book2.xlsx')

ws2 = wb2['Sheet1']

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

ws2.merge_cells('D'+ str(i) + ':F' + str(i))

# B2セルに[中央揃え]を設定

ws2['D' + str(i)].alignment = Alignment(horizontal='center')

  i += 1

#bブックを「copy4」で保存する
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