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
| class MonthlyFinanceAutomation: """月度财务分析全自动系统""" def __init__(self, output_folder='月度报表'): self.output_folder = output_folder os.makedirs(output_folder, exist_ok=True) self.data = None self.report_file = None def load_data(self, source='auto'): """ 加载数据(支持:CSV/Excel/模拟数据) source: 'auto' - 自动查找data文件夹 'mock' - 生成模拟数据 """ if source == 'mock': print("📊 生成模拟财务数据...") self.data = self._generate_mock_data() print(f"✅ 生成 {len(self.data)} 条记录") return data_files = [] if os.path.exists('data'): data_files.extend([f for f in os.listdir('data') if f.endswith(('.csv', '.xlsx'))]) if not data_files: print("⚠️ 未找到数据文件,使用模拟数据") self.data = self._generate_mock_data() return latest_file = sorted(data_files)[-1] filepath = os.path.join('data', latest_file) print(f"📄 加载数据文件: {latest_file}") try: if latest_file.endswith('.csv'): self.data = pd.read_csv(filepath) else: self.data = pd.read_excel(filepath) print(f"✅ 加载成功: {len(self.data)} 行,{len(self.data.columns)} 列") except Exception as e: print(f"❌ 加载失败: {e},将使用模拟数据") self.data = self._generate_mock_data() def _generate_mock_data(self): """生成模拟财务数据""" np.random.seed(42) n_records = 1000 data = { '日期': pd.date_range('2025-01-01', periods=n_records, freq='D'), '部门': np.random.choice(['销售部', '市场部', '技术部', '财务部'], n_records), '产品': np.random.choice(['产品A', '产品B', '服务C', '服务D', '产品E'], n_records), '客户类型': np.random.choice(['新客户', '老客户', 'VIP客户'], n_records, p=[0.3, 0.5, 0.2]), '收入': np.random.randint(5000, 50000, n_records), '成本': np.random.randint(2000, 25000, n_records), '费用': np.random.randint(500, 8000, n_records) } df = pd.DataFrame(data) df['利润'] = df['收入'] - df['成本'] - df['费用'] df['利润率'] = df['利润'] / df['收入'] return df def generate_report(self, month='2025-01'): """ 生成完整月度财务分析报告 """ if self.data is None: print("⚠️ 请先加载数据") return print(f"\n{'='*60}") print(f"开始生成 {month} 月度财务分析报告") print(f"{'='*60}") monthly_data = self.data[self.data['日期'].dt.strftime('%Y-%m') == month] if monthly_data.empty: print(f"⚠️ 没有找到 {month} 的数据") return self.report_file = os.path.join( self.output_folder, f"财务分析报告_{month}.xlsx" ) print("\n1️⃣ 写入基础数据...") with pd.ExcelWriter(self.report_file, engine='xlsxwriter') as writer: monthly_data.to_excel(writer, sheet_name='原始数据', index=False) print("2️⃣ 创建汇总指标...") summary_data = { '指标': [ '月度总收入', '月度总成本', '月度总费用', '月度净利润', '平均利润率', '交易笔数', '日均收入', '最高单日收入' ], '金额': [ monthly_data['收入'].sum(), monthly_data['成本'].sum(), monthly_data['费用'].sum(), monthly_data['利润'].sum(), monthly_data['利润率'].mean(), len(monthly_data), monthly_data.groupby(monthly_data['日期'].dt.day)['收入'].sum().mean(), monthly_data.groupby(monthly_data['日期'].dt.day)['收入'].sum().max() ], '单位': ['元', '元', '元', '元', '%', '笔', '元', '元'] } summary_df = pd.DataFrame(summary_data) summary_df.to_excel(writer, sheet_name='汇总指标', index=False) print("3️⃣ 生成数据透视表...") pivot_dept = monthly_data.pivot_table( values=['收入', '利润'], index='部门', aggfunc='sum', margins=True, margins_name='总计' ) pivot_dept.to_excel(writer, sheet_name='部门分析') pivot_product = monthly_data.pivot_table( values=['收入', '利润', '利润率'], index='产品', aggfunc={'收入': 'sum', '利润': 'sum', '利润率': 'mean'} ) pivot_product.to_excel(writer, sheet_name='产品分析') pivot_customer = monthly_data.pivot_table( values=['收入', '利润'], index='客户类型', columns='部门', aggfunc='sum', fill_value=0 ) pivot_customer.to_excel(writer, sheet_name='客户分析') print("✅ 数据写入完成") print("4️⃣ 美化报表格式...") self._beautify_report() print("5️⃣ 插入分析图表...") self._add_charts_to_report() print(f"\n🎉 月度财务分析报告生成完成!") print(f"📊 文件位置: {self.report_file}") def _beautify_report(self): """美化报表格式""" wb = load_workbook(self.report_file) ws_summary = wb['汇总指标'] ws_summary.column_dimensions['A'].width = 20 ws_summary.column_dimensions['B'].width = 18 ws_summary.column_dimensions['C'].width = 10 header_font = Font(bold=True, color="FFFFFF", size=11) header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid") header_align = Alignment(horizontal="center", vertical="center") money_font = Font(color="006100") money_fill = PatternFill(start_color="C6EFCE", end_color="C6EFCE", fill_type="solid") for row in range(1, ws_summary.max_row + 1): for col in range(1, ws_summary.max_column + 1): cell = ws_summary.cell(row=row, column=col) if row == 1: cell.font = header_font cell.fill = header_fill cell.alignment = header_align elif col == 2: cell.font = money_font cell.fill = money_fill cell.number_format = '#,##0.00' elif col == 3: cell.alignment = Alignment(horizontal="center") thin_border = Border( left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin') ) for row in ws_summary.iter_rows(min_row=1, max_row=ws_summary.max_row, min_col=1, max_col=ws_summary.max_column): for cell in row: cell.border = thin_border wb.save(self.report_file) def _add_charts_to_report(self): """添加图表""" wb = load_workbook(self.report_file) ws_data = wb['原始数据'] ws_chart = wb.create_sheet(title='图表分析', index=0) from openpyxl.chart import BarChart, LineChart, Reference ws_dept = wb['部门分析'] chart = BarChart() chart.title = "各部门收入与利润对比" chart.style = 10 chart.y_axis.title = '金额(元)' chart.x_axis.title = '部门' data = Reference(ws_dept, min_col=2, min_row=1, max_row=ws_dept.max_row, max_col=3) categories = Reference(ws_dept, min_col=1, min_row=2, max_row=ws_dept.max_row) chart.add_data(data, titles_from_data=True) chart.set_categories(categories) ws_chart.add_chart(chart, "A1") from openpyxl.chart import PieChart pie = PieChart() pie.title = "客户类型收入占比" ws_customer = wb['客户分析'] pie_data = Reference(ws_customer, min_col=2, min_row=1, max_row=ws_customer.max_row) pie_cat = Reference(ws_customer, min_col=1, min_row=2, max_row=ws_customer.max_row) pie.add_data(pie_data, titles_from_data=True) pie.set_categories(pie_cat) ws_chart.add_chart(pie, "J1") wb.save(self.report_file) def run_automation(self): """运行完整自动化流程""" print("🤖 启动月度财务分析自动化流程") self.load_data('auto') month = input("请输入月份(如2025-01,回车使用数据中的月份): ").strip() or '2025-01' self.generate_report(month) print("\n🚀 流程执行完毕!报表已生成并保存")
def main(): """主菜单""" automation = MonthlyFinanceAutomation() print("=" * 60) print("月度财务分析自动化系统") print("一键完成:加载数据 → 生成报表 → 插入透视表 → 添加图表") print("=" * 60) while True: print("\n" + "=" * 45) print("功能菜单") print("=" * 45) print("1. 加载数据") print("2. 生成月度报表") print("3. 运行完整自动化") print("4. 创建示例数据文件") print("5. 退出") print("=" * 45) choice = input("请选择: ").strip() if choice == "1": source = input("数据来源(auto/mock): ").strip() or 'auto' automation.load_data(source) elif choice == "2": if automation.data is None: print("⚠️ 请先加载数据") continue month = input("月份(如2025-01): ").strip() or '2025-01' automation.generate_report(month) elif choice == "3": automation.run_automation() elif choice == "4": os.makedirs("data", exist_ok=True) mock_data = automation._generate_mock_data() mock_data.to_excel("data/2025-01-财务数据.xlsx", index=False) print("✅ 示例数据已创建: data/2025-01-财务数据.xlsx") elif choice == "5": print("👋 感谢使用,自动化让工作更轻松!") break else: print("请输入1-5!")
if __name__ == "__main__": main()
|