2016年6月6日月曜日

開発環境

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

Practice Projects(Spreadsheet Cell Inverter)(No. 7198)

コード(Emacs)

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

import openpyxl
from openpyxl.cell import get_column_letter

wb_before = openpyxl.load_workbook('before.xlsx')
wb_after = openpyxl.Workbook()
sheet_before = wb_before.active
sheet_after = wb_after.active

for x in range(sheet_before.max_row):
    for y in range(sheet_before.max_column):
        before = '{0}{1}'.format(chr(ord('A') + y), x + 1)
        after = '{0}{1}'.format(get_column_letter(x + 1), y + 1)
        sheet_after[after] = sheet_before[before].value

wb_after.save('after.xlsx')

入出力結果(Terminal, IPython)

$ ./cell_inverter.py
$ open before.xlsx after.xlsx
$

0 コメント:

コメントを投稿