1
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?

こちらのプログラムを汎用的に使えるように改造しました。
Openpyxl: Cell の結合

プログラム

merge_cells.py
#! /usr/bin/python
# ---------------------------------------------------------------
#	merge_cells.py
#
#					Jun/14/2024
# ---------------------------------------------------------------
import sys
import openpyxl
from openpyxl.styles import Alignment

# ---------------------------------------------------------------
# [4-6]:
def column_analize(ws,column):
	print("ws.max_row = ",ws.max_row)
	print("column = ",column)
	istart = []
	iend = []
	nn = 0
	icount = 1
	for row in ws:
		if row[column].value != None:
#			print(row[column].value)
			istart.append(icount)
			if 0 < nn:
				iend.append(icount - 1)
			nn += 1
#		else:
#			print("None") 

		icount += 1
#
	iend.append(icount-1)
#
	print(istart)
	print(iend)
#
	return istart,iend
# ---------------------------------------------------------------
# [4]:
def column_proc(column_in):
	istart, iend = column_analize(ws,column_in - 1)
	for it in range(len(istart)):
		ws.merge_cells(start_row=istart[it],
		start_column=column_in, end_row=iend[it], end_column=column_in)
		ws.cell(istart[it], column_in).alignment \
			= Alignment(horizontal='center',vertical='center')
# ---------------------------------------------------------------
xlsx_in=sys.argv[1]
#
sys.stderr.write("xlsx_in = %s\n" % xlsx_in)
wb = openpyxl.load_workbook(filename=xlsx_in)
ws = wb.worksheets[0]
#
column_proc(1)
column_proc(2)
column_proc(3)
#
wb.save(xlsx_in)
# ---------------------------------------------------------------

実行方法

./merge_cells.py ex01.xlsx
1
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
1
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?