開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Automate the Boring Stuff with Python (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 12.(Working with Excel Spreadsheets)、Practice Projects(Blank Row Inserter)(No. 7183)を取り組んでみる。
Practice Projects(Blank Row Inserter)(No. 7183)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import openpyxl
from openpyxl.styles import Font, Style
try:
n = int(sys.argv[1])
m = int(sys.argv[2])
filename = sys.argv[3]
except Exception as err:
print(err)
sys.exit(1)
wb_before = openpyxl.load_workbook(filename)
wb_after = openpyxl.Workbook()
sheet_before = wb_before.active
sheet_after = wb_after.active
sheet_after.title = sheet_before.title
for i in range(n):
for j in range(sheet_before.max_column):
cell = '{0}{1}'.format(chr(ord('A') + j), i + 1)
sheet_after[cell] = sheet_before[cell].value
for i in range(n, sheet_before.max_row):
for j in range(sheet_before.max_column):
cell_before = '{0}{1}'.format(chr(ord('A') + j), i + 1)
cell_after = '{0}{1}'.format(chr(ord('A') + j), i + m + 1)
sheet_after[cell_after] = sheet_before[cell_before].value
wb_after.save(filename)
入出力結果(Terminal, IPython)
$ ./blank_row_inserter.py 2 3 multiplication_table10.xlsx $ open multiplication_table10.xlsx $
0 コメント:
コメントを投稿