1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
| print("\n=== 专业年度报告系统 ===")
class 年度报告生成器: """专业年度报告生成器""" def __init__(self): self.年度数据 = None self.分析报告 = {} self.图表文件 = [] def 加载数据(self, 收入数据, 支出数据, 结余数据): """加载年度财务数据""" self.年度收入 = 收入数据 self.年度支出 = 支出数据 self.年度结余 = 结余数据 self.计算年度汇总() print("✅ 年度数据加载完成") def 计算年度汇总(self): """计算年度关键指标""" self.年度汇总 = { '年度总收入': self.年度收入['总收入'].sum(), '年度总支出': self.年度支出['总支出'].sum(), '年度总结余': self.年度结余['月度结余'].sum(), '平均月结余': self.年度结余['月度结余'].mean(), '平均结余率': self.年度结余['结余率'].mean(), '收入结构': self.年度收入[['工资收入', '投资收益', '其他收入']].sum(), '支出结构': self.年度支出.drop(['月份', '总支出'], axis=1).sum() } self.计算详细分析() def 计算详细分析(self): """进行详细的数据分析""" self.月度排名 = { '收入最高月份': self.年度收入.loc[self.年度收入['总收入'].idxmax(), '月份'], '收入最低月份': self.年度收入.loc[self.年度收入['总收入'].idxmin(), '月份'], '支出最高月份': self.年度支出.loc[self.年度支出['总支出'].idxmax(), '月份'], '支出最低月份': self.年度支出.loc[self.年度支出['总支出'].idxmin(), '月份'], '结余最高月份': self.年度结余.loc[self.年度结余['月度结余'].idxmax(), '月份'], '结余最低月份': self.年度结余.loc[self.年度结余['月度结余'].idxmin(), '月份'] } self.趋势分析 = { '收入趋势': '上升' if self.年度收入['总收入'].iloc[-1] > self.年度收入['总收入'].iloc[0] else '下降', '支出趋势': '上升' if self.年度支出['总支出'].iloc[-1] > self.年度支出['总支出'].iloc[0] else '下降', '结余趋势': '改善' if self.年度结余['月度结余'].iloc[-1] > self.年度结余['月度结余'].iloc[0] else '恶化' } self.健康度评估() def 健康度评估(self): """评估财务健康度""" 平均结余率 = self.年度汇总['平均结余率'] if 平均结余率 >= 25: self.健康等级 = "优秀" self.健康得分 = 90 self.健康建议 = "您的财务状况非常优秀!建议继续保持,并考虑增加投资。" elif 平均结余率 >= 20: self.健康等级 = "良好" self.健康得分 = 80 self.健康建议 = "您的财务状况良好!建议适当优化支出结构,提高储蓄效率。" elif 平均结余率 >= 15: self.健康等级 = "一般" self.健康得分 = 70 self.健康建议 = "您的财务状况一般,建议控制非必要支出,提高结余率。" else: self.健康等级 = "需改善" self.健康得分 = 60 self.健康建议 = "您的财务状况需要改善,建议制定严格的预算计划,减少不必要的开支。" self.健康指标 = { '结余率得分': min(100, 平均结余率 * 4), '收入稳定性得分': max(0, 100 - (self.年度收入['总收入'].std() / self.年度收入['总收入'].mean() * 100)), '支出控制得分': max(0, 100 - (self.年度支出['总支出'].std() / self.年度支出['总支出'].mean() * 100)), } self.健康指标['综合得分'] = sum(self.健康指标.values()) / len(self.健康指标) def 生成图表集(self): """生成完整的图表集""" print("📊 生成专业图表集...") self.图表集 = plt.figure(figsize=(20, 16)) gs = self.图表集.add_gridspec(4, 4, hspace=0.4, wspace=0.3) ax1 = self.图表集.add_subplot(gs[0, :2]) self._绘制年度总览(ax1) ax2 = self.图表集.add_subplot(gs[0, 2]) self._绘制收入结构(ax2) ax3 = self.图表集.add_subplot(gs[0, 3]) self._绘制支出结构(ax3) ax4 = self.图表集.add_subplot(gs[1, :2]) self._绘制月度趋势(ax4) ax5 = self.图表集.add_subplot(gs[1, 2:]) self._绘制热力图(ax5) ax6 = self.图表集.add_subplot(gs[2, :2]) self._绘制结余率分析(ax6) ax7 = self.图表集.add_subplot(gs[2, 2]) self._绘制收入对比(ax7) ax8 = self.图表集.add_subplot(gs[2, 3]) self._绘制支出对比(ax8) ax9 = self.图表集.add_subplot(gs[3, :2]) self._绘制健康度雷达图(ax9) ax10 = self.图表集.add_subplot(gs[3, 2:]) self._绘制关键指标(ax10) self.图表集.suptitle(f'个人理财年度报告 2024\n财务健康度:{self.健康等级}({self.健康得分}分)', fontsize=28, fontweight='bold', y=0.98) self.图表集.text(0.5, 0.02, f'报告生成时间:{datetime.now().strftime("%Y年%m月%d日 %H:%M")} | ' + f'年度总结余:{self.年度汇总["年度总结余"]:,}元 | ' + f'平均结余率:{self.年度汇总["平均结余率"]:.1f}%', ha='center', fontsize=14, style='italic', bbox=dict(boxstyle='round,pad=0.5', facecolor='lightgray', alpha=0.8), transform=self.图表集.transFigure) plt.tight_layout() 图表文件 = '个人理财年度报告_专业版.png' plt.savefig(图表文件, dpi=300, bbox_inches='tight', facecolor='white') plt.show() self.图表文件.append(图表文件) print("✅ 专业图表集已生成!") return 图表文件 def _绘制年度总览(self, ax): """绘制年度总览图""" ax.plot(self.年度收入['月份'], self.年度收入['总收入'], marker='o', linewidth=4, label='总收入', color='#2E8B57', markersize=8) ax.plot(self.年度支出['月份'], self.年度支出['总支出'], marker='s', linewidth=4, label='总支出', color='#DC143C', markersize=8) ax.fill_between(self.年度收入['月份'], self.年度收入['总收入'], alpha=0.3, color='#2E8B57') ax.fill_between(self.年度支出['月份'], self.年度支出['总支出'], alpha=0.3, color='#DC143C') ax.text(0.02, 0.98, f'总收入:{self.年度汇总["年度总收入"]:,}元', transform=ax.transAxes, fontsize=12, verticalalignment='top', bbox=dict(boxstyle='round', facecolor='lightgreen', alpha=0.8)) ax.text(0.02, 0.88, f'总支出:{self.年度汇总["年度总支出"]:,}元', transform=ax.transAxes, fontsize=12, verticalalignment='top', bbox=dict(boxstyle='round', facecolor='lightcoral', alpha=0.8)) ax.set_title('年度收支总览', fontsize=16, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('金额(元)', fontsize=12) ax.legend(fontsize=12, loc='upper right') ax.grid(True, alpha=0.3) def _绘制收入结构(self, ax): """绘制收入结构图""" 收入结构 = self.年度汇总['收入结构'] wedges, texts, autotexts = ax.pie(收入结构.values, labels=收入结构.index, autopct='%1.1f%%', startangle=90, colors=['#3498DB', '#2ECC71', '#F39C12']) ax.set_title('年度收入结构', fontsize=14, fontweight='bold') for autotext in autotexts: autotext.set_color('white') autotext.set_fontweight('bold') def _绘制支出结构(self, ax): """绘制支出结构图""" 支出结构 = self.年度汇总['支出结构'] wedges, texts, autotexts = ax.pie(支出结构.values, labels=支出结构.index, autopct='%1.1f%%', startangle=90, colors=['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FECA57', '#FF9FF3']) ax.set_title('年度支出结构', fontsize=14, fontweight='bold') for autotext in autotexts: autotext.set_color('white') autotext.set_fontweight('bold') def _绘制月度趋势(self, ax): """绘制月度趋势对比图""" 收支对比 = pd.DataFrame({ '月份': self.年度收入['月份'], '收入': self.年度收入['总收入'], '支出': self.年度支出['总支出'], '结余': self.年度结余['月度结余'] }) ax_twin = ax.twinx() 收支对比.plot(x='月份', y=['收入', '支出'], kind='line', ax=ax, color=['#2E8B57', '#DC143C'], linewidth=3, marker='o', markersize=6) ax_twin.plot(收支对比['月份'], 收支对比['结余'], color='#4169E1', linewidth=3, marker='^', markersize=6, label='结余') ax.set_title('月度收支趋势对比', fontsize=16, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('收入/支出(元)', fontsize=12) ax_twin.set_ylabel('结余(元)', fontsize=12) ax.legend(loc='upper left') ax_twin.legend(loc='upper right') ax.grid(True, alpha=0.3) def _绘制热力图(self, ax): """绘制收支热力图""" 支出明细 = self.年度支出.drop(['月份', '总支出'], axis=1) 热力图数据 = 支出明细.T sns.heatmap(热力图数据, annot=True, fmt='d', cmap='RdYlGn_r', ax=ax, cbar_kws={'label': '金额(元)'}, linewidths=0.5) ax.set_title('月度支出热力图', fontsize=16, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('支出类别', fontsize=12) def _绘制结余率分析(self, ax): """绘制结余率分析图""" bars = ax.bar(self.年度结余['月份'], self.年度结余['结余率'], color=['#2ECC71' if x >= 20 else '#F39C12' if x >= 10 else '#E74C3C' for x in self.年度结余['结余率']]) ax.set_title('月度结余率分析', fontsize=16, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('结余率(%)', fontsize=12) ax.axhline(y=20, color='green', linestyle='--', alpha=0.7, label='目标结余率(20%)') ax.axhline(y=self.年度汇总['平均结余率'], color='blue', linestyle='--', alpha=0.7, label=f'平均结余率({self.年度汇总["平均结余率"]:.1f}%)') ax.legend() for bar, 数值 in zip(bars, self.年度结余['结余率']): ax.annotate(f'{数值:.1f}%', (bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5), ha='center', va='bottom', fontsize=10, fontweight='bold') def _绘制收入对比(self, ax): """绘制收入对比图""" 收入对比 = self.年度收入.set_index('月份')[['工资收入', '投资收益', '其他收入']] 收入对比.plot(kind='bar', ax=ax, color=['#3498DB', '#2ECC71', '#F39C12'], width=0.8) ax.set_title('各类收入月度对比', fontsize=14, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('收入金额(元)', fontsize=12) ax.legend(fontsize=10) ax.tick_params(axis='x', rotation=45) ax.grid(True, alpha=0.3) def _绘制支出对比(self, ax): """绘制支出对比图""" 支出对比 = self.年度支出.set_index('月份').drop(['总支出'], axis=1) 支出对比.plot(kind='bar', ax=ax, stacked=True, color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FECA57', '#FF9FF3'], width=0.8) ax.set_title('各类支出月度对比', fontsize=14, fontweight='bold') ax.set_xlabel('月份', fontsize=12) ax.set_ylabel('支出金额(元)', fontsize=12) ax.legend(fontsize=9, loc='upper left', bbox_to_anchor=(1, 1)) ax.tick_params(axis='x', rotation=45) ax.grid(True, alpha=0.3) def _绘制健康度雷达图(self, ax): """绘制健康度雷达图""" 指标列表 = list(self.健康指标.keys()) 数值列表 = list(self.健康指标.values()) 指标列表 += [指标列表[0]] 数值列表 += [数值列表[0]] 角度 = np.linspace(0, 2 * np.pi, len(指标列表), endpoint=True) ax.plot(角度, 数值列表, 'o-', linewidth=3, color='#3498DB') ax.fill(角度, 数值列表, alpha=0.3, color='#3498DB') ax.set_xticks(角度[:-1]) ax.set_xticklabels(指标列表[:-1]) ax.set_ylim(0, 100) ax.grid(True) ax.set_title('财务健康度评估', fontsize=14, fontweight='bold') def _绘制关键指标(self, ax): """绘制关键指标仪表盘""" ax.axis('off') 关键指标文本 = f""" 关键财务指标 年度总收入:{self.年度汇总["年度总收入"]:,.0f}元 年度总支出:{self.年度汇总["年度总支出"]:,.0f}元 年度总结余:{self.年度汇总["年度总结余"]:,.0f}元 平均结余率:{self.年度汇总["平均结余率"]:.1f}% 财务健康等级:{self.健康等级} 健康度评分:{self.健康得分}分 收入最高月份:{self.月度排名["收入最高月份"]} 支出最高月份:{self.月度排名["支出最高月份"]} 结余最高月份:{self.月度排名["结余最高月份"]} """ ax.text(0.5, 0.5, 关键指标文本, transform=ax.transAxes, fontsize=12, ha='center', va='center', bbox=dict(boxstyle='round,pad=0.5', facecolor='lightblue', alpha=0.8)) def 生成完整报告(self, 输出目录="年度报告"): """生成完整的年度报告包""" print("📦 生成完整年度报告包...") import os os.makedirs(输出目录, exist_ok=True) 图表文件 = self.生成图表集() Excel文件 = self.生成Excel报告(os.path.join(输出目录, "年度财务数据.xlsx")) PDF文件 = self.生成PDF报告(os.path.join(输出目录, "年度报告.pdf")) 文本文件 = self.生成文本报告(os.path.join(输出目录, "年度报告摘要.txt")) Markdown文件 = self.生成Markdown报告(os.path.join(输出目录, "年度报告.md")) print(f"✅ 完整年度报告包已生成:{输出目录}/") print(f" 📊 图表文件:{图表文件}") print(f" 📈 Excel文件:{Excel文件}") print(f" 📄 PDF文件:{PDF文件}") print(f" 📝 文本文件:{文本文件}") print(f" 📝 Markdown文件:{Markdown文件}") return { '图表': 图表文件, 'Excel': Excel文件, 'PDF': PDF文件, '文本': 文本文件, 'Markdown': Markdown文件 } def 生成Excel报告(self, 文件名): """生成Excel格式的详细报告""" print(f"📊 生成Excel报告:{文件名}") with pd.ExcelWriter(文件名, engine='openpyxl') as writer: self.年度收入.to_excel(writer, sheet_name='年度收入', index=False) self.年度支出.to_excel(writer, sheet_name='年度支出', index=False) self.年度结余.to_excel(writer, sheet_name='年度结余', index=False) 汇总表 = pd.DataFrame({ '项目': ['年度总收入', '年度总支出', '年度总结余', '平均月结余', '平均结余率', '财务健康等级', '健康度得分'], '数值': [self.年度汇总["年度总收入"], self.年度汇总["年度总支出"], self.年度汇总["年度总结余"], self.年度汇总["平均月结余"], self.年度汇总["平均结余率"], self.健康等级, self.健康得分], '单位': ['元', '元', '元', '元', '%', '', '分'] }) 汇总表.to_excel(writer, sheet_name='年度汇总', index=False) 分析结果 = pd.DataFrame({ '指标': ['收入最高月份', '支出最高月份', '结余最高月份', '收入趋势', '支出趋势', '结余趋势'], '数值': [self.月度排名["收入最高月份"], self.月度排名["支出最高月份"], self.月度排名["结余最高月份"], self.趋势分析["收入趋势"], self.趋势分析["支出趋势"], self.趋势分析["结余趋势"]] }) 分析结果.to_excel(writer, sheet_name='分析结果', index=False) 健康指标表 = pd.DataFrame({ '健康指标': list(self.健康指标.keys()), '得分': list(self.健康指标.values()), '权重': [1/len(self.健康指标)] * len(self.健康指标) }) 健康指标表.to_excel(writer, sheet_name='健康指标', index=False) return 文件名 def 生成PDF报告(self, 文件名): """生成PDF格式的专业报告""" print(f"📄 生成PDF报告:{文件名}") from matplotlib.backends.backend_pdf import PdfPages with PdfPages(文件名) as pdf: self._生成PDF封面(pdf) self._生成PDF总览(pdf) self._生成PDF详细分析(pdf) self._生成PDF健康评估(pdf) self._生成PDF建议(pdf) return 文件名 def _生成PDF封面(self, pdf): """生成PDF封面""" fig = plt.figure(figsize=(8.27, 11.69)) plt.text(0.5, 0.8, '个人理财年度报告', ha='center', va='center', transform=fig.transFigure, fontsize=36, fontweight='bold') plt.text(0.5, 0.65, '2024年度财务分析报告', ha='center', va='center', transform=fig.transFigure, fontsize=24, style='italic') plt.text(0.5, 0.45, f'年度总结余:{self.年度汇总["年度总结余"]:,.0f}元', ha='center', va='center', transform=fig.transFigure, fontsize=20, color='#2ECC71', fontweight='bold', bbox=dict(boxstyle='round', facecolor='lightgreen', alpha=0.8)) plt.text(0.5, 0.35, f'平均结余率:{self.年度汇总["平均结余率"]:.1f}%', ha='center', va='center', transform=fig.transFigure, fontsize=18, color='#3498DB', fontweight='bold', bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.8)) plt.text(0.5, 0.25, f'财务健康度:{self.健康等级}', ha='center', va='center', transform=fig.transFigure, fontsize=18, color='#DC143C', fontweight='bold', bbox=dict(boxstyle='round', facecolor='lightcoral', alpha=0.8)) plt.text(0.5, 0.1, f'生成时间:{datetime.now().strftime("%Y年%m月%d日")}', ha='center', va='center', transform=fig.transFigure, fontsize=14) plt.axis('off') pdf.savefig(fig, bbox_inches='tight') plt.close() def _生成PDF总览(self, pdf): """生成PDF总览页""" if hasattr(self, '图表集'): pdf.savefig(self.图表集, bbox_inches='tight') else: fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(11.69, 8.27)) self._绘制年度总览(ax1) self._绘制收入结构(ax2) self._绘制支出结构(ax3) self._绘制月度趋势(ax4) plt.suptitle('年度财务总览', fontsize=18, fontweight='bold') plt.tight_layout() pdf.savefig(fig, bbox_inches='tight') plt.close() def _生成PDF详细分析(self, pdf): """生成PDF详细分析页""" fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(11.69, 8.27)) self._绘制结余率分析(ax1) self._绘制收入对比(ax2) self._绘制支出对比(ax3) self._绘制热力图(ax4) plt.suptitle('详细财务分析', fontsize=18, fontweight='bold') plt.tight_layout() pdf.savefig(fig, bbox_inches='tight') plt.close() def _生成PDF健康评估(self, pdf): """生成PDF健康度评估页""" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11.69, 8.27)) self._绘制健康度雷达图(ax1) self._绘制关键指标(ax2) plt.suptitle('财务健康度评估', fontsize=18, fontweight='bold') plt.tight_layout() pdf.savefig(fig, bbox_inches='tight') plt.close() def _生成PDF建议(self, pdf): """生成PDF建议页""" fig = plt.figure(figsize=(8.27, 11.69)) 建议内容 = f""" 个人理财建议与规划 一、当前财务状况总结 1. 总体评价 • 财务健康等级:{self.健康等级} • 健康度评分:{self.健康得分}分 • 平均结余率:{self.年度汇总["平均结余率"]:.1f}% 2. 主要亮点 • 年度总结余:{self.年度汇总["年度总结余"]:,.0f}元 • 收入结构:工资收入占比{self.年度汇总["收入结构"]["工资收入"]/self.年度汇总["年度总收入"]*100:.1f}% 3. 需要关注的问题 • 支出集中度:{list(self.年度汇总["支出结构"].keys())[0]}占比最高 • 结余率波动:{self.年度结余["结余率"].std():.1f}% 二、明年理财目标 1. 结余目标 • 目标结余率:≥25% • 目标月结余:≥{self.年度汇总["平均月结余"]*1.2:,.0f}元 2. 收入目标 • 增加投资收入占比至10%以上 • 探索多元化收入来源 3. 支出控制目标 • 降低非必要支出占比 • 建立应急基金(6个月生活费) 三、具体行动计划 1. 短期行动(1-3个月) • 制定详细的月度预算 • 建立支出追踪系统 • 开始建立应急基金 2. 中期行动(3-12个月) • 学习投资理财知识 • 优化投资组合 • 定期回顾和调整计划 3. 长期行动(1年以上) • 实现财务目标多元化 • 建立被动收入来源 • 规划退休理财方案 """ plt.text(0.1, 0.9, 建议内容, transform=fig.transFigure, fontsize=12, verticalalignment='top', fontfamily='monospace', bbox=dict(boxstyle='round,pad=0.5', facecolor='lightyellow', alpha=0.8)) plt.axis('off') plt.suptitle('个人理财建议与规划', fontsize=20, fontweight='bold', y=0.98) pdf.savefig(fig, bbox_inches='tight') plt.close() def 生成文本报告(self, 文件名): """生成文本格式的报告摘要""" print(f"📝 生成文本报告:{文件名}") 报告内容 = f""" ═══════════════════════════════════════════════════════════════ 个人理财年度报告摘要 2024年度 ═══════════════════════════════════════════════════════════════ 📊 年度财务概况 ─────────────────────────────────────────────────────────────── • 年度总收入:{self.年度汇总["年度总收入"]:,}元 • 年度总支出:{self.年度汇总["年度总支出"]:,}元 • 年度总结余:{self.年度汇总["年度总结余"]:,}元 • 平均月结余:{self.年度汇总["平均月结余"]:,.2f}元 • 平均结余率:{self.年度汇总["平均结余率"]:.1f}% • 财务健康等级:{self.健康等级}({self.健康得分}分) 📈 趋势分析 ─────────────────────────────────────────────────────────────── • 收入趋势:{self.趋势分析["收入趋势"]} • 支出趋势:{self.趋势分析["支出趋势"]} • 结余趋势:{self.趋势分析["结余趋势"]} 🏆 年度之最 ─────────────────────────────────────────────────────────────── • 收入最高月份:{self.月度排名["收入最高月份"]} • 支出最高月份:{self.月度排名["支出最高月份"]} • 结余最高月份:{self.月度排名["结余最高月份"]} 💡 理财建议 ─────────────────────────────────────────────────────────────── {self.健康建议} 📅 报告生成时间:{datetime.now().strftime("%Y年%m月%d日 %H:%M")} ═══════════════════════════════════════════════════════════════ """ with open(文件名, 'w', encoding='utf-8') as f: f.write(报告内容) return 文件名 def 生成Markdown报告(self, 文件名): """生成Markdown格式的报告""" print(f"📝 生成Markdown报告:{文件名}") Markdown内容 = f"""# 个人理财年度报告 2024 ## 📊 年度财务概况 ### 关键指标 | 指标 | 数值 | 单位 | |------|------|------| | 年度总收入 | {self.年度汇总["年度总收入"]:,.0f} | 元 | | 年度总支出 | {self.年度汇总["年度总支出"]:,.0f} | 元 | | 年度总结余 | {self.年度汇总["年度总结余"]:,.0f} | 元 | | 平均月结余 | {self.年度汇总["平均月结余"]:,.2f} | 元 | | 平均结余率 | {self.年度汇总["平均结余率"]:.1f} | % | | 财务健康等级 | {self.健康等级} | - | | 健康度得分 | {self.健康得分} | 分 | ### 收入结构 {self.年度汇总["收入结构"].to_markdown()} ### 支出结构 {self.年度汇总["支出结构"].to_markdown()} ## 📈 趋势分析 ### 月度趋势 {self.年度结余[["月份", "月度结余", "结余率"]].to_markdown()} ### 关键排名 - 收入最高月份:{self.月度排名["收入最高月份"]} - 支出最高月份:{self.月度排名["支出最高月份"]} - 结余最高月份:{self.月度排名["结余最高月份"]} ## 💡 理财建议 {self.健康建议} ## 📊 健康指标详情 {pd.DataFrame(list(self.健康指标.items()), columns=['指标', '得分']).to_markdown()} --- *报告生成时间:{datetime.now().strftime("%Y年%m月%d日 %H:%M")}* """ with open(文件名, 'w', encoding='utf-8') as f: f.write(Markdown内容) return 文件名
专业生成器 = 年度报告生成器()
专业生成器.加载数据(月度收入, 月度支出, 月度结余)
报告文件 = 专业生成器.生成完整报告("个人理财年度报告_专业版")
print("\n🎉 所有报告已生成完成!") print("您可以查看以下文件:") for 类型, 文件 in 报告文件.items(): print(f" {类型}:{文件}")
|