Excelファイルを自動生成する
Windows上でExcelを操作して生成
COM経由でExcelを操作して生成します。
pywin32モジュールとExcelが必要です。
# -*- encoding:cp932 -*- from win32com import client app = client.Dispatch('Excel.Application') app.Visible = 0 app.Workbooks.Add() book = app.Workbooks(1) sheet = book.Sheets(1) sheet.Cells(1,1).Value = 'Excelファイル自動生成' sheet.Cells(2,1).Value = '商品A' sheet.Cells(2,2).Value = '580' sheet.Cells(2,3).Value = '2' sheet.Cells(2,4).Value = '=B2*C2' sheet.Cells(3,1).Value = '商品B' sheet.Cells(3,2).Value = '1980' sheet.Cells(3,3).Value = '1' sheet.Cells(3,4).Value = '=B3*C3' sheet.Cells(4,1).Value = '商品C' sheet.Cells(4,2).Value = '7980' sheet.Cells(4,3).Value = '1' sheet.Cells(4,4).Value = '=B4*C4' sheet.Cells(5,1).Value = '合計' sheet.Cells(5,3).Value = '=SUM(C2:C4)' sheet.Cells(5,4).Value = '=SUM(D2:D4)' book.SaveAs(Filename='receipt.xls') app.Quit()