abc_parser.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import pandas as pd
  2. from tkinter import messagebox
  3. from flow_parser_base import FlowParserBase
  4. class ABCFlowParser(FlowParserBase):
  5. """农业银行流水解析器"""
  6. def parse(self, flow_file_path):
  7. """解析农行流水文件,返回标准化的流水数据"""
  8. try:
  9. flow_df = pd.read_excel(flow_file_path, dtype=str, header=2)
  10. flow_df.columns = flow_df.columns.str.strip()
  11. required_columns = ["交易时间", "收入金额", "支出金额", "账户余额", "对方账号", "对方户名", "对方开户行", "摘要"]
  12. missing_fields = [k for k in required_columns if k not in flow_df.columns]
  13. if missing_fields:
  14. messagebox.showerror("错误", f"农行流水缺少必要列:{missing_fields}")
  15. return None
  16. filtered_df = flow_df.copy()
  17. filtered_df = filtered_df[filtered_df["交易时间"].str.contains(r"\d{4}-\d{2}-\d{2}", na=False)]
  18. filtered_df["交易时间"] = pd.to_datetime(filtered_df["交易时间"], errors='coerce')
  19. filtered_df = filtered_df.sort_values("交易时间", ascending=True)
  20. standard_flow = []
  21. for _, row in filtered_df.iterrows():
  22. transaction_time = row["交易时间"]
  23. income_amount = pd.to_numeric(row["收入金额"], errors='coerce')
  24. expense_amount = pd.to_numeric(row["支出金额"], errors='coerce')
  25. balance = str(row["账户余额"]).strip()
  26. opponent_account = str(row["对方账号"]).strip()
  27. opponent_name = str(row["对方户名"]).strip()
  28. opponent_bank = str(row["对方开户行"]).strip()
  29. summary = str(row["摘要"]).strip()
  30. income = income_amount if pd.notna(income_amount) and income_amount > 0 else 0
  31. expense = expense_amount if pd.notna(expense_amount) and expense_amount > 0 else 0
  32. if summary == "费用外收":
  33. summary = "手续费"
  34. opponent_name = "手续费"
  35. opponent_bank = "手续费"
  36. transaction_time_str = transaction_time.strftime("%Y-%m-%d %H:%M:%S") if pd.notna(transaction_time) else ""
  37. standard_flow.append({
  38. "交易时间": transaction_time_str,
  39. "对方户名": opponent_name,
  40. "摘要": summary,
  41. "收入": income,
  42. "支出": expense,
  43. "对方开户机构": opponent_bank,
  44. "备注": opponent_bank,
  45. "余额": balance
  46. })
  47. return pd.DataFrame(standard_flow)
  48. except Exception as e:
  49. messagebox.showerror("错误", f"解析农行流水失败:{str(e)}")
  50. return None
  51. def get_bank_name(self):
  52. """返回银行名称"""
  53. return "农业银行"