>
自动化完整的报告生命周期:数据提取 → 格式化 → 图表生成 → PDF渲染 → 分发。
| 报告 | 频率 | 主要受众 |
|---|---|---|
| 损益表(利润表) | 月度/季度 | CEO、董事会 |
| 资产负债表 |
识别源系统并提取原始数据:
bash
python
def processpl(inputcsv, period):
将损益表原始数据处理为结构化格式。
categories = defaultdict(float)
with open(input_csv) as f:
reader = csv.DictReader(f)
for row in reader:
categories[row[Category]] += float(row[Amount] or 0)
revenue = sum(v for k, v in categories.items() if Revenue in k or Income in k)
cogs = sum(v for k, v in categories.items() if COGS in k or Cost of in k)
gross_profit = revenue - cogs
expenses = sum(v for k, v in categories.items() if k not in [Revenue, COGS])
netincome = grossprofit - expenses
return {
period: period,
revenue: revenue,
cogs: cogs,
grossprofit: grossprofit,
grossmargin: (grossprofit / revenue * 100) if revenue else 0,
expenses: expenses,
netincome: netincome,
netmargin: (netincome / revenue * 100) if revenue else 0,
categories: dict(categories)
}
python
def revenuetrendchart(datapoints, outputpath):
生成收入趋势折线图。
periods = [d[period] for d in data_points]
revenues = [d[revenue] for d in data_points]
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(periods, revenues, b-o, linewidth=2, markersize=8)
ax.fill_between(periods, revenues, alpha=0.1)
ax.yaxis.setmajorformatter(mticker.FuncFormatter(lambda x, _: f${x:,.0f}))
ax.set_title(收入趋势, fontsize=14, fontweight=bold)
ax.set_xlabel(期间)
ax.set_ylabel(收入)
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(outputpath, dpi=150, bboxinches=tight)
plt.close()
return output_path
def expensebreakdownchart(categories, output_path):
生成费用类别饼图。
expense_cats = {k: v for k, v in categories.items()
if v > 0 and Revenue not in k and Income not in k}
labels = list(expense_cats.keys())
values = list(expense_cats.values())
fig, ax = plt.subplots(figsize=(8, 8))
wedges, texts, autotexts = ax.pie(
values, labels=labels, autopct=%1.1f%%,
startangle=90, pctdistance=0.85
)
ax.set_title(费用细分, fontsize=14, fontweight=bold)
plt.tight_layout()
plt.savefig(outputpath, dpi=150, bboxinches=tight)
plt.close()
return output_path
def variancebarchart(budgetvsactual, output_path):
生成预算与实际差异对比图。
categories = list(budgetvsactual.keys())
budgets = [budgetvsactual[c][budget] for c in categories]
actuals = [budgetvsactual[c][actual] for c in categories]
x = np.arange(len(categories))
width = 0.35
fig, ax = plt.subplots(figsize=(12, 6))
bars1 = ax.bar(x - width/2, budgets, width, label=预算, color=steelblue)
bars2 = ax.bar(x + width/2, actuals, width, label=实际, color=coral)
ax.set_title(预算 vs 实际, fontsize=14, fontweight=bold)
ax.set_xticks(x)
ax.set_xticklabels(categories, rotation=45, ha=right)
ax.yaxis.setmajorformatter(mticker.FuncFormatter(lambda x, _: f${x:,.0f}))
ax.legend()
ax.grid(True, alpha=0.3, axis=y)
plt.tight_layout()
plt.savefig(outputpath, dpi=150, bboxinches=tight)
plt.close()
return output_path
python
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import (
SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image, PageBreak
)
from reportlab.lib.enums import TACENTER, TARIGHT
from datetime import datetime
def generateplreport(data, chartpaths, outputpath, company_name=PrecisionLedger客户):
生成完整的损益表PDF报告。
doc = SimpleDocTemplate(
output_path,
pagesize=letter,
rightMargin=0.75*inch,
leftMargin=0.75*inch,
topMargin=0.75*inch,
bottomMargin=0.75*inch
)
styles = getSampleStyleSheet()
# 自定义样式
title_style = ParagraphStyle(
Title, parent=styles[Title],
fontSize=20, textColor=colors.HexColor(#1a1a2e),
spaceAfter=6
)
subtitle_style = ParagraphStyle(
Subtitle, parent=styles[Normal],
fontSize=11, textColor=colors.HexColor(#666666),
spaceAfter=20, alignment=TA_CENTER
)
section_style = ParagraphStyle(
Section, parent=styles[Heading2],
fontSize=13, textColor=colors.HexColor(#1a1a2e),
spaceBefore=16, spaceAfter=8,
borderPad=4
)
story = []
# 页眉
story.append(Paragraph(companyname, titlestyle))
story.append(Paragraph(
f损益表 — {data[period]}, subtitle_style
))
story.append(Paragraph(
f生成日期:{datetime.now().strftime(%Y年%m月%d日)},
ParagraphStyle(gen_date, parent=styles[Normal],
fontSize=9, textColor=colors.grey, alignment=TA_CENTER)
))
story.append(Spacer(1, 0.25*inch))
# 关键指标汇总表
story.append(Paragraph
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 pl-report-generator-1776071892 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 pl-report-generator-1776071892 技能
skillhub install pl-report-generator-1776071892
文件大小: 6.13 KB | 发布时间: 2026-4-15 13:54