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?

Openpyxl: Border のコピー

Posted at

こちらのページを参考にしました。
罫線を別のシートやブックにコピーする[openpyxl]

11列目からの6列の罫線を、17列目以降にコピーする例です。

プログラム

copy_borders.py
#! /usr/bin/python
# ---------------------------------------------------------------
#	copy_borders.py
#
#					Aug/06/2024
# ---------------------------------------------------------------
import sys
import openpyxl
from openpyxl.styles.borders import Border, Side
#
# ---------------------------------------------------------------
def copy_cell_border(row_in,col_src,col_target):
	left_border = ws.cell(column=col_src, row=row_in).border.left.border_style
	right_border = ws.cell(column=col_src, row=row_in).border.right.border_style
	top_border = ws.cell(column=col_src, row=row_in).border.top.border_style
	bottom_border = ws.cell(column=col_src, row=row_in).border.bottom.border_style

	ws.cell(column=col_target, row=row_in).border = Border(left=Side(style=left_border), right=Side(style=right_border), top=Side(style=top_border), bottom=Side(style=bottom_border))
#
#
# ---------------------------------------------------------------
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]
print(ws.max_row)
#
col_from = 11
col_to = 17
for row_in in range(2,ws.max_row):
	for it in range(6):
		copy_cell_border(row_in,col_from+it,col_to+it)
#
wb.save(xlsx_in)
# ---------------------------------------------------------------

実行コマンド

./copy_borders.py in01.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?