1 / 21
📊

Part 07 Unit 03

實戰專案:批次報告製作

一次生成多份個人化報告

適用週報、月報、客戶報告

2 / 21

專案目標

建立批次報告製作系統:

  • 從資料自動生成報告
  • 統一的報告模板
  • 支援個人化內容
  • 批次輸出多份報告

💡 100 份報告也能一鍵完成!

3 / 21

應用場景

場景說明
員工週報每週自動生成各部門報告
客戶報告個人化的服務報告
學生成績單批次生成成績報告
專案狀態多專案的進度報告
4 / 21

步驟 1:準備資料來源

建立 CSV 資料檔

name,department,completed,pending,highlight 王小明,研發部,15,3,完成新功能開發 李小美,設計部,12,2,品牌視覺更新 張小華,行銷部,20,5,Q4活動策劃 陳小強,業務部,18,4,達成季度目標
5 / 21

步驟 2:建立報告模板

--- title: 週報 - {{name}} date: {{date}} department: {{department}} --- # {{name}} 週報 ## 基本資訊 - **姓名**:{{name}} - **部門**:{{department}} - **週期**:{{date}} ## 本週成果 - 完成任務數:**{{completed}}** 項 - 待處理任務:**{{pending}}** 項 ## 工作亮點 {{highlight}} ## 下週計畫 (待填寫) --- *報告自動生成於 {{generated}}*
6 / 21

步驟 3:批次生成腳本

# generate-reports.ps1 param( [string]$DataFile = "data.csv", [string]$Template = "template.md" ) $data = Import-Csv $DataFile $templateContent = Get-Content $Template -Raw $date = Get-Date -Format "yyyy/MM/dd" $generated = Get-Date -Format "yyyy-MM-dd HH:mm" foreach ($row in $data) { $report = $templateContent $report = $report -replace '\{\{name\}\}', $row.name $report = $report -replace '\{\{department\}\}', $row.department $report = $report -replace '\{\{completed\}\}', $row.completed $report = $report -replace '\{\{pending\}\}', $row.pending $report = $report -replace '\{\{highlight\}\}', $row.highlight $report = $report -replace '\{\{date\}\}', $date $report = $report -replace '\{\{generated\}\}', $generated $outputFile = "output\report-$($row.name).md" $report | Out-File $outputFile -Encoding UTF8 Write-Host "✅ 生成: $outputFile" -ForegroundColor Green }
7 / 21

步驟 4:轉換成 Word

# 批次轉換所有報告 Get-ChildItem output\*.md | ForEach-Object { $output = $_.BaseName + ".docx" pandoc $_.FullName -o "output\$output" ` --reference-doc=templates\report-template.docx Write-Host "✅ $output" -ForegroundColor Green }
8 / 21

步驟 5:合併成完整腳本

# full-report-pipeline.ps1 # 完整報告生成流程 param( [string]$DataFile = "data.csv" ) Write-Host "📊 開始生成報告..." -ForegroundColor Cyan # 1. 生成 Markdown .\generate-reports.ps1 -DataFile $DataFile # 2. 轉換成 Word $mdFiles = Get-ChildItem output\*.md $total = $mdFiles.Count $current = 0 foreach ($file in $mdFiles) { $current++ $percent = [math]::Round(($current / $total) * 100) Write-Progress -Activity "轉換中" -Status "$percent%" -PercentComplete $percent pandoc $file.FullName -o ($file.FullName -replace '\.md$', '.docx') ` --reference-doc=templates\report-template.docx } # 3. 清理 Markdown 暫存檔(可選) # Remove-Item output\*.md Write-Host "`n✅ 完成!共生成 $total 份報告" -ForegroundColor Green Write-Host "📁 輸出位置: output\" -ForegroundColor Cyan
9 / 21

進階:AI 生成個人化內容

請根據以下資料,為每位員工撰寫個人化的週報摘要: 員工資料: - 王小明:完成 15 項任務,亮點是新功能開發 - 李小美:完成 12 項任務,亮點是品牌更新 要求: 1. 每人 2-3 句話的摘要 2. 語氣積極正面 3. 提及具體成就 4. 適當給予肯定
10 / 21

進階:從 Excel 讀取資料

# 需要安裝 ImportExcel 模組 # Install-Module ImportExcel # 從 Excel 讀取資料 $data = Import-Excel "data.xlsx" # 之後的處理與 CSV 相同 foreach ($row in $data) { # 生成報告... }

💡 ImportExcel 模組可以直接讀取 .xlsx

11 / 21

進階:加入圖表

使用 Mermaid 語法(轉 HTML 時)

## 任務完成比例 ```mermaid pie title 任務狀態 "已完成" : {{completed}} "待處理" : {{pending}} ```
# 轉換時啟用 Mermaid pandoc report.md -o report.html --standalone -F mermaid-filter
12 / 21

進階:Email 自動發送

# send-reports.ps1 # 使用 Outlook 發送報告 $outlook = New-Object -ComObject Outlook.Application $reports = Get-ChildItem output\*.docx foreach ($report in $reports) { # 從檔名提取收件人(假設格式:report-姓名.docx) $name = $report.BaseName -replace 'report-', '' # 查找 email(需要對照表) $email = $emailList[$name] $mail = $outlook.CreateItem(0) $mail.Subject = "週報 - $name" $mail.Body = "您好,附件為本週報告,請查收。" $mail.To = $email $mail.Attachments.Add($report.FullName) $mail.Send() Write-Host "📧 已發送: $name" -ForegroundColor Green }
13 / 21

專案完整流程

  1. 準備資料(CSV/Excel)
  2. 設計報告模板(Markdown)
  3. 執行生成腳本
  4. 批次轉換格式
  5. (選用)自動發送
# 一鍵執行 .\full-report-pipeline.ps1 -DataFile weekly-data.csv
14 / 21

效率比較

報告數量手動製作自動化
1 份30 分鐘1 分鐘
10 份5 小時1 分鐘
100 份50 小時2 分鐘

🚀 數量越多,節省越多!

15 / 21

動手練習

完成以下任務:

  1. 建立包含 3 筆資料的 CSV
  2. 建立簡單的報告模板
  3. 撰寫生成腳本
  4. 批次生成 3 份報告
16 / 21

練習答案

🔒 輸入密碼查看答案

# 1. data.csv "name,score 小明,95 小美,88 小華,92" | Out-File data.csv # 2. template.md "# {{name}} 成績報告`n分數:{{score}}" | Out-File template.md # 3-4. 生成腳本 $data = Import-Csv data.csv $tpl = Get-Content template.md -Raw foreach ($r in $data) { $report = $tpl -replace '\{\{name\}\}',$r.name -replace '\{\{score\}\}',$r.score $report | Out-File "report-$($r.name).md" }
17 / 21

常見問題

問題解決方案
CSV 中文亂碼儲存為 UTF-8 編碼
變數沒替換檢查 {{}} 格式
輸出檔名重複加入唯一識別碼
模板格式跑掉檢查 Markdown 語法
18 / 21

擴展可能

  • 📅 排程自動執行(Windows 工作排程器)
  • 📊 從資料庫讀取資料
  • 🌐 Web 介面上傳資料
  • ☁️ 雲端儲存和分享
  • 📱 發送通知提醒
19 / 21

排程自動執行

# 使用 Windows 工作排程器 # 1. 開啟「工作排程器」 # 2. 建立基本工作 # 3. 設定觸發條件(每週一 9:00) # 4. 動作:啟動程式 # 程式:powershell.exe # 引數:-File "D:\Reports\full-report-pipeline.ps1"

💡 設定一次,永久自動執行!

20 / 21

本單元總結

專案成果:

  • 完整的批次報告生成系統
  • 從資料到報告全自動化
  • 支援任意數量的報告
  • 可擴展為排程自動執行
21 / 21

Part 07 總結

三個實戰專案完成!

  • ✅ 會議記錄自動化
  • ✅ 技術文件產生器
  • ✅ 批次報告製作

🎉 你已經掌握了 AI + Pandoc 的核心技能!

下一個 Part:進階技巧與優化 ⚡