boc_parser.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import pandas as pd
  2. from tkinter import messagebox
  3. from flow_parser_base import FlowParserBase
  4. class BOCFlowParser(FlowParserBase):
  5. """中国银行流水解析器"""
  6. def parse(self, flow_file_path):
  7. """解析中国银行流水文件,返回标准化的流水数据"""
  8. try:
  9. flow_df = pd.read_excel(flow_file_path, dtype=str, header=8)
  10. flow_df.columns = flow_df.columns.str.strip()
  11. required_columns = [
  12. "交易类型[ Transaction Type ]",
  13. "交易日期[ Transaction Date ]",
  14. "交易时间[ Transaction time ]",
  15. "交易金额[ Trade Amount ]",
  16. "交易后余额[ After-transaction balance ]",
  17. "摘要[ Reference ]",
  18. "收款人名称[ Payee's Name ]",
  19. "收款人开户行名[ Beneficiary account bank ]",
  20. "付款人名称[ Payer's Name ]",
  21. "付款人开户行名[ Payer account bank ]"
  22. ]
  23. missing_fields = [k for k in required_columns if k not in flow_df.columns]
  24. if missing_fields:
  25. messagebox.showerror("错误", f"中国银行流水缺少必要列:{missing_fields}")
  26. return None
  27. standard_flow = []
  28. for _, row in flow_df.iterrows():
  29. transaction_type = str(row["交易类型[ Transaction Type ]"]).strip()
  30. transaction_date = str(row["交易日期[ Transaction Date ]"]).strip()
  31. transaction_time = str(row["交易时间[ Transaction time ]"]).strip()
  32. amount = pd.to_numeric(row["交易金额[ Trade Amount ]"], errors='coerce')
  33. balance = str(row["交易后余额[ After-transaction balance ]"]).strip()
  34. reference = str(row["摘要[ Reference ]"]).strip()
  35. payee_name = str(row["收款人名称[ Payee's Name ]"]).strip()
  36. payee_bank = str(row["收款人开户行名[ Beneficiary account bank ]"]).strip()
  37. payer_name = str(row["付款人名称[ Payer's Name ]"]).strip()
  38. payer_bank = str(row["付款人开户行名[ Payer account bank ]"]).strip()
  39. if pd.isna(amount) or amount == 0:
  40. continue
  41. if transaction_type == "往账":
  42. income = 0
  43. expense = abs(amount)
  44. opponent_name = payee_name
  45. opponent_bank = payee_bank
  46. elif transaction_type == "来账":
  47. income = abs(amount)
  48. expense = 0
  49. opponent_name = payer_name
  50. opponent_bank = payer_bank
  51. else:
  52. continue
  53. transaction_datetime = f"{transaction_date} {transaction_time}"
  54. standard_flow.append({
  55. "交易时间": transaction_datetime,
  56. "对方户名": opponent_name,
  57. "摘要": reference,
  58. "收入": income,
  59. "支出": expense,
  60. "对方开户机构": opponent_bank,
  61. "备注": opponent_bank,
  62. "余额": balance
  63. })
  64. return pd.DataFrame(standard_flow)
  65. except Exception as e:
  66. messagebox.showerror("错误", f"解析中国银行流水失败:{str(e)}")
  67. return None
  68. def get_bank_name(self):
  69. """返回银行名称"""
  70. return "中国银行"