""" 报告生成器使用示例 展示各种使用场景 """ import os import sys # 添加父目录到路径 current_dir = os.path.dirname(os.path.abspath(__file__)) parent_dir = os.path.dirname(os.path.dirname(current_dir)) if parent_dir not in sys.path: sys.path.insert(0, parent_dir) from applications.reporter.daily import DailyReporter from applications.reporter.weekly import WeeklyReporter def example_daily_report(): """示例1:生成简单日报""" print("=" * 50) print("示例1:生成日报(使用内置模板)") print("=" * 50) reporter = DailyReporter() report_path = reporter.generate() print(f"✅ 日报已生成: {report_path}\n") def example_weekly_report(): """示例2:生成简单周报""" print("=" * 50) print("示例2:生成周报(使用内置模板)") print("=" * 50) reporter = WeeklyReporter() report_path = reporter.generate() print(f"✅ 周报已生成: {report_path}\n") def example_custom_template(): """示例3:使用外部模板""" print("=" * 50) print("示例3:使用外部HTML模板生成日报") print("=" * 50) # 获取模板路径(相对于当前文件) template_path = os.path.join( os.path.dirname(__file__), 'templates', 'custom_template_example.html' ) if os.path.exists(template_path): reporter = DailyReporter() report_path = reporter.generate(template_path=template_path) print(f"✅ 使用外部模板生成的日报: {report_path}\n") else: print(f"⚠️ 模板文件不存在: {template_path}\n") def example_custom_output_dir(): """示例4:指定输出目录""" print("=" * 50) print("示例4:指定自定义输出目录") print("=" * 50) reporter = DailyReporter() custom_dir = "output/reports/custom" report_path = reporter.generate(output_dir=custom_dir) print(f"✅ 报告已保存到自定义目录: {report_path}\n") def example_add_data_source(): """示例5:添加自定义数据源""" print("=" * 50) print("示例5:添加自定义数据源") print("=" * 50) try: from applications.reporter.data_source_example import ComplaintDataSource from utils.mysql_agent import MySQLAgent from config import Config reporter = DailyReporter() # 添加投诉数据源(如果数据库中有该表) db_agent = MySQLAgent(Config.MYSQL_CONFIG) complaint_source = ComplaintDataSource(db_agent, table_name="complaint_data") reporter.add_data_source(complaint_source) report_path = reporter.generate() print(f"✅ 包含自定义数据源的报告已生成: {report_path}\n") except Exception as e: print(f"⚠️ 添加自定义数据源失败(可能是表不存在): {str(e)}\n") def main(): """主函数""" print("\n" + "=" * 50) print("汽车后市场情报报告生成器 - 使用示例") print("=" * 50 + "\n") # 运行各种示例 try: example_daily_report() except Exception as e: print(f"❌ 示例1失败: {str(e)}\n") try: example_weekly_report() except Exception as e: print(f"❌ 示例2失败: {str(e)}\n") try: example_custom_template() except Exception as e: print(f"❌ 示例3失败: {str(e)}\n") try: example_custom_output_dir() except Exception as e: print(f"❌ 示例4失败: {str(e)}\n") try: example_add_data_source() except Exception as e: print(f"❌ 示例5失败: {str(e)}\n") print("=" * 50) print("所有示例运行完成!") print("=" * 50 + "\n") if __name__ == "__main__": main()