エクセルの自動化(work)

pythonと自動化

  1. HOME
  2. EXCEL

請求書をテンプレートに自動生成

ビジネス書類はどの会社でも特定のフォーマットが決まっているでしょうから、プログラミングでテンプレートにデータを流し込むだけという状態にしていきます。

今回は下記のようなテンプレの請求書を用意して自動生成させていきます。

請求書テンプレ

請求書の自動生成

import openpyxl as excel
template_file = "invoice-template.xlsx"
save_file = "invoice01.xlsx"

name = "株式会社〇〇九州"
subject = "1月分のご請求"
items = [
    ["コンサルティング代金一式", 1, 120000],
    ["システム構築費", 1, 200000],
    ["LP制作費", 4, 25000]
]

book = excel.load_workbook(template_file)
sheet = book.active
sheet["B4"] = name
sheet["C10"] = subject

# 内訳を繰り返し書き込んでいく
total = 0
for i, it in enumerate(items):
    summary, count, price = it
    subtotal = count * price
    total += subtotal

    row = 15 + i
    sheet.cell(row, 2, summary)
    sheet.cell(row, 5, count)
    sheet.cell(row, 6, price)
    sheet.cell(row, 7, subtotal)
sheet["C11"] = total

book.save(save_file)

請求書