python3にてEXCELファイルを出力するサンプル
puthon3にて、EXCELをとりあつかう際、openpyxlなるものを利用しました。
すごく簡単だったです
インストール
$ sudo pip-3.6 install openpyxl
宣言
import openpyxl as px
EXCELファイルの出力サンプル
下記サンプルは、DBから取得した値をEXCELのシートに出力する例になります。一行目は、ヘッダ、以降はレコードと言った感じです。
wb = px.Workbook() ws = wb.active y=1 ws.cell(row=y, column=1).value = "name" ws.cell(row=y, column=2).value = "mail" ws.cell(row=y, column=3).value = "createdat" ws.cell(row=y, column=4).value = "titile" ws.cell(row=y, column=5).value = "detail" ws.cell(row=y, column=6).value = "path" for row in c: y+=1 print(row) ws.cell(row=y, column=1).value = row['name'] ws.cell(row=y, column=2).value = row['mail'] ws.cell(row=y, column=3).value = row['createdat'] ws.cell(row=y, column=4).value = row['title'] ws.cell(row=y, column=5).value = row['detail'] ws.cell(row=y, column=6).value = row['path'] wb.save('/tmp/excelfile_' + _strValue + '.xlsx')
参考
openpyxl - A Python library to read/write Excel 2010 xlsx/xlsm files — openpyxl 2.5.0 documentation