0
0

Openpyxl: シートのコピー

Last updated at Posted at 2024-05-31

file_a.xlsx のシート1を、file_b.xlsx にコピーする方法です

プログラム

sheet_copy.py
#! /usr/bin/python
# ---------------------------------------------------------------
#	sheet_copy.py
#
#					May/31/2024
# ---------------------------------------------------------------
import sys
import openpyxl
from openpyxl.styles import Border, Side, Alignment, Font

# ---------------------------------------------------------------
xlsx_in=sys.argv[1]
xlsx_out=sys.argv[2]
#
sheettitle = 'コード一覧'

wb1 = openpyxl.load_workbook(filename=xlsx_in)
ws1 = wb1.worksheets[1]

wb2 = openpyxl.load_workbook(filename=xlsx_out)
ws2 = wb2.worksheets[1]
ws2.title = sheettitle

for row in ws1.row_dimensions:
	ws2.row_dimensions[row].height = ws1.row_dimensions[row].height
#
ws2['C2'].value = ws1['C2'].value
icount = 1
for row in ws1:
	if 5 < icount:
		for cell in row:
			ws2[cell.coordinate].value = cell.value
			border_spec1 = Side(color='000000', style='thin')
			ws2[cell.coordinate].border = Border(top=border_spec1, bottom=border_spec1, left=border_spec1, right=border_spec1)
			ws2[cell.coordinate].alignment = Alignment(horizontal = 'center', vertical = 'center', wrap_text = True)
	icount += 1

wb2.save(xlsx_out)
#
# ---------------------------------------------------------------

実行コマンド

/sheet_copy.py file_a.xlsx file_b.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