0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python業務自動化】 Excelレポート作成・広告分析・RFM分析を自動化する方法

0
Last updated at Posted at 2026-03-09

毎月のレポート作成やデータ分析に
時間がかかっていませんか?

Pythonでできる業務自動化まとめ

Excel・広告分析・RFM分析・A/Bテスト・スクレイピング

日々の業務では

・Excel集計
・レポート作成
・広告データ分析
・顧客分析

などの作業が多く発生します。

これらはPythonを使うことでかなり自動化できます。

この記事では実務で使える Python業務自動化のサンプルコードをまとめます。


① レポート作成の自動化(Excel → グラフ → PowerPoint)

売上レポートなどでは

  • Excel集計
  • グラフ作成
  • PowerPoint資料作成

といった作業が必要になります。

Pythonを使えばこれらをすべて自動化できます。


①-1 複数Excelファイルを自動統合し、前月比を計算する

複数の売上Excelファイルをまとめて読み込み、
月ごとの売上合計と前月比を計算します。

import pandas as pd
from glob import glob

# Excelファイル一覧取得
files = glob("data/sales_2025_*.xlsx")

df_list = []

# 各Excelを読み込む
for f in files:
    df = pd.read_excel(f)
    df_list.append(df)

# データ結合
all_data = pd.concat(df_list, ignore_index=True)

# 月別売上集計
monthly = (
    all_data
    .groupby("month")["sales"]
    .sum()
    .reset_index()
)

# 前月売上
monthly["prev_month_sales"] = monthly["sales"].shift(1)

# 前月比
monthly["growth_rate"] = (
    (monthly["sales"] / monthly["prev_month_sales"]) - 1
) * 100

# Excel出力
monthly.to_excel("output/monthly_report.xlsx", index=False)

これで月次レポートが自動生成できます。


①-2 売上グラフを自動生成

レポートにはグラフも必要になります。
matplotlib を使えば自動作成できます。

import matplotlib.pyplot as plt

plt.figure(figsize=(8,4))

plt.plot(
    monthly["month"],
    monthly["sales"],
    marker="o"
)

plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")

plt.savefig("output/sales_graph.png")

plt.close()

このコードで売上グラフを画像として保存できます。


①-3 PowerPoint資料を自動作成

作成したグラフをPowerPointに自動貼り付けできます。

from pptx import Presentation
from pptx.util import Inches

prs = Presentation()

slide = prs.slides.add_slide(
    prs.slide_layouts[5]
)

slide.shapes.title.text = "月次売上レポート"

slide.shapes.add_picture(
    "output/sales_graph.png",
    left=Inches(1),
    top=Inches(2),
    width=Inches(6)
)

prs.save("output/monthly_report.pptx")

これで

Excel → グラフ → PowerPoint

まで自動化できます。


② 広告データ分析の効率化

広告分析では

  • CPA
  • CTR
  • CVR

などの指標計算が必要です。

Pythonなら自動計算できます。


Google広告CSVの分析

import pandas as pd

df = pd.read_csv("google_ads.csv")

# CPA
df["CPA"] = df["cost"] / df["conversions"]

# CTR
df["CTR"] = df["clicks"] / df["impressions"] * 100

# CVR
df["CVR"] = df["conversions"] / df["clicks"] * 100

# CPAが良い順
df = df.sort_values("CPA")

df.to_excel("output/ads_report.xlsx", index=False)

広告レポートが自動作成されます。


異常値検出

広告費が急に変化した場合などを検出できます。

df["cost_change"] = df["cost"].pct_change()

abnormal = df[
    df["cost_change"].abs() > 0.5
]

50%以上変動したデータを抽出します。


③ 顧客分析(RFM分析)

顧客を分析する方法として有名なのが RFM分析です。

  • Recency(最近購入)
  • Frequency(購入回数)
  • Monetary(購入金額)

import pandas as pd
import datetime as dt

today = dt.datetime(2025,1,31)

rfm = df.groupby("customer_id").agg({
    "order_date": lambda x: (today - x.max()).days,
    "order_id": "count",
    "amount": "sum"
}).reset_index()

rfm.columns = [
    "customer_id",
    "Recency",
    "Frequency",
    "Monetary"
]

rfm["Rank"] = pd.qcut(
    rfm["Monetary"],
    3,
    labels=["Low","Mid","High"]
)

顧客のランク分けができます。


④ A/Bテスト分析

Webマーケティングでは
A/Bテストの結果分析が必要になります。

from scipy.stats import chi2_contingency

table = [
    [a_conversions, a_visitors - a_conversions],
    [b_conversions, b_visitors - b_conversions]
]

chi2, p, _, _ = chi2_contingency(table)

if p < 0.05:
    result = "有意差あり"
else:
    result = "有意差なし"

統計的に有意差があるか判定できます。


⑤ Webスクレイピング

競合価格や商品情報の取得もPythonでできます。


価格取得

import requests
from bs4 import BeautifulSoup

url = "https://example.com/product"

response = requests.get(url)

soup = BeautifulSoup(response.text,"html.parser")

price = soup.select_one(".price").text

口コミ分析

from collections import Counter

texts = df["review"].dropna()

all_text = " ".join(texts)

words = all_text.split()

Counter(words).most_common(10)

頻出ワードを抽出できます。


⑥ Excel業務の自動クリーニング

Excelデータでは

  • 表記ゆれ
  • 重複

がよく問題になります。


表記統一

import unicodedata

df["name"] = df["name"].apply(
    lambda x: unicodedata.normalize("NFKC",x)
)

重複削除

df = df.drop_duplicates()

CSV → Excel一括変換

from glob import glob
import pandas as pd

files = glob("csv/*.csv")

for f in files:
    
    df = pd.read_csv(f)
    
    df.to_excel(
        f.replace(".csv",".xlsx"),
        index=False
    )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?