[ python ] Converting CSV file into MS-Excel file
The module file can be downloaded here. ( Right click and select "save ..." )
# -*- coding: utf-8 -*-
import sys, csv
import pyExcelerator
# About csv module, see "http://www.python.jp/doc/2.4/lib/module-csv.html"
# pyExcelerator can be redistributed with BSD Lisence and all rights reserved Roman V. Kiseliov
# The project page of pyExcelerator is "http://sourceforge.net/projects/pyexcelerator/"
def convert( csv_fobj, excel_file_name ):
"""
convert( csv_fobj, excel_file_name )
Converts a csv file into MS-Excel file.
First argument: csv_fobj is a file object which reference to csv file.
Second argument: excel_file_name is a file name.
"""
record_from_csv = csv.reader( csv_fobj )
excel_workbook = pyExcelerator.Workbook()
excel_sheets = [sheet for sheet in map( excel_workbook.add_sheet, [u"sheet" + str( index ) for index in range( 3 )] )]
(row, column) = (0, 0)
for fields in record_from_csv:
for cell_val in fields:
excel_sheets[0].write( row, column, label=cell_val )
column += 1
column = 0
row += 1
excel_workbook.save( excel_file_name )
if __name__ == "__main__":
if( sys.argv[1] in ("-h", "/h", "-?", "/?", "--help") ) or ( len( sys.argv ) < 3 ):
print "Usage: python csv_to_excel.py csvfile excelfile"
csv_fobj = open( sys.argv[1], "rb" )
convert( csv_fobj, sys.argv[2] )
CSV から MS-EXCEL ファイルに変換するモジュールが見つからなかったので作ってみました。
ダウンロードはこちらからどうぞ。
ダウンロードできない場合は右クリックメニューから「対象を保存」を実行してください。
コマンドラインからも実行できます。コマンドラインから実行するときは次のように入力してください。 入力コマンド中の csvfile は CSV ファイル名、excelfile は MS-EXCEL のファイル名です。なお、既存のファイルに追記することはできません。 既存のファイル名を指定すると上書きされてしまうのでご注意ください。
python csv_to_xls.py csvfile excelfile
- Reference
- [python][office]Python2.5でExcelを使わずにExcelファイルを読み書きする
( 2006/12/06, seraphy )- Py Excelerator Project Page ( Since 2005/03/16, Roman V. Kiseliov )
| 固定リンク
この記事へのコメントは終了しました。
コメント