2016年6月4日土曜日

開発環境

Automate the Boring Stuff with Python (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 12.(Working with Excel Spreadsheets)、Practice Projects(Multiplication Table Maker)(No. 7183)を取り組んでみる。

Practice Projects(Multiplication Table Maker)(No. 7183)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import openpyxl
from openpyxl.styles import Font, Style

wb = openpyxl.Workbook()
sheet = wb.active
try:
    n = int(sys.argv[1])
except Exception as err:
    print(err)
    sys.exit(1)

font = Font(bold=True)    
style = Style(font=font)
for i in range(n):
    cell = '{0}1'.format(chr(ord('B') + i))
    sheet[cell].style = style
    sheet[cell] = i + 1
    cell = 'A{0}'.format(i + 2)
    sheet[cell].style = style
    sheet[cell] = i + 1

for i in range(1, n + 1):
    for j in range(1, n + 1):
        cell = '{0}{1}'.format(chr(ord('A') + i), j + 1)
        sheet[cell] = i * j

wb.save('multiplication_table{0}.xlsx'.format(n))

入出力結果(Terminal, IPython)

$ ./multiplicationTable.py 6
$ ./multiplicationTable.py 10

0 コメント:

コメントを投稿