2016年6月8日水曜日

開発環境

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

Practice Projects(Text Files to Spreadsheet)(No. 7212)

コード(Emacs)

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

import sys
import openpyxl
from openpyxl.cell import get_column_letter

n = 0
wb = openpyxl.Workbook()
sheet = wb.active
for col, filename in enumerate(sys.argv[1:], 1):
    with open(filename) as f:
        lines = f.readlines()
    for row, line in enumerate(lines, 1):
        sheet['{0}{1}'.format(get_column_letter(col), row)] = line
    n += 1

wb.save('text.xlsx')

入出力結果(Terminal, IPython)

$ cat text1.txt 
text1 line1
text1 line2
text1 line3
text1 line4
text1 line5
$ cat text2.txt 
text2 line1
text2 line2
text2 line3
text2 line4
text2 line5
text2 line6
text2 line7
text2 line8
text2 line9
text2 line10
$ ./text2spreadsheet.py text1.txt text2.txt 
$ open text.xlsx
$

0 コメント:

コメントを投稿

Comments on Google+: