95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
||
"""发票RPA功能演示脚本。
|
||
|
||
使用方法:
|
||
1. 首先获取浏览器配置目录:
|
||
python account-manager/scripts/main.py ensure-web --platform sandbox-jc2009
|
||
|
||
2. 运行演示:
|
||
python demo_rpa.py --profile-dir "从account-manager获取的profile_dir"
|
||
"""
|
||
|
||
import argparse
|
||
import asyncio
|
||
import sys
|
||
import os
|
||
|
||
# 添加scripts目录到Python路径
|
||
scripts_dir = os.path.join(os.path.dirname(__file__), "scripts")
|
||
if scripts_dir not in sys.path:
|
||
sys.path.insert(0, scripts_dir)
|
||
|
||
from service.rpa import run_invoice_rpa
|
||
|
||
|
||
async def main():
|
||
"""主函数。"""
|
||
parser = argparse.ArgumentParser(description="发票RPA功能演示")
|
||
parser.add_argument("--profile-dir", required=True, help="浏览器配置目录")
|
||
parser.add_argument("--username", default="15527406659", help="用户名")
|
||
parser.add_argument("--password", default="qwer1234", help="密码")
|
||
parser.add_argument("--headless", action="store_true", help="无头模式")
|
||
parser.add_argument("--detail", action="store_true", help="提取详细信息")
|
||
|
||
args = parser.parse_args()
|
||
|
||
print("=" * 60)
|
||
print("发票RPA功能演示")
|
||
print("=" * 60)
|
||
print(f"目标网站: https://sandbox.jc2009.com/industries/campus/smartcampus/app/finance/invoice-precheck")
|
||
print(f"用户名: {args.username[:3]}***")
|
||
print(f"浏览器配置: {args.profile_dir}")
|
||
print(f"模式: {'无头' if args.headless else '有头'}")
|
||
print(f"提取模式: {'详细' if args.detail else '列表'}")
|
||
print("=" * 60)
|
||
|
||
try:
|
||
print("\n正在启动浏览器并登录...")
|
||
result = await run_invoice_rpa(
|
||
username=args.username,
|
||
password=args.password,
|
||
profile_dir=args.profile_dir,
|
||
headless=args.headless,
|
||
detail_mode=args.detail,
|
||
)
|
||
|
||
if result.success:
|
||
print("\n✅ 发票信息提取成功!")
|
||
print(f"提取到 {result.data.get('total_count', 0)} 张发票信息")
|
||
|
||
# 显示前3条发票信息
|
||
invoices = result.data.get('invoices', [])
|
||
if invoices:
|
||
print("\n前3条发票信息:")
|
||
for i, invoice in enumerate(invoices[:3]):
|
||
print(f"\n发票 {i+1}:")
|
||
print(f" 行文本: {invoice.get('row_text', 'N/A')[:100]}...")
|
||
if 'invoice_number' in invoice:
|
||
print(f" 发票号码: {invoice['invoice_number']}")
|
||
if 'amount' in invoice:
|
||
print(f" 金额: {invoice['amount']}")
|
||
|
||
# 保存完整结果到文件
|
||
output_file = "invoice_extraction_result.json"
|
||
import json
|
||
with open(output_file, 'w', encoding='utf-8') as f:
|
||
json.dump(result.data, f, ensure_ascii=False, indent=2)
|
||
print(f"\n完整结果已保存到: {output_file}")
|
||
|
||
else:
|
||
print(f"\n❌ 发票信息提取失败!")
|
||
print(f"错误信息: {result.error}")
|
||
print(f"错误阶段: {result.stage}")
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ 运行异常: {str(e)}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
print("\n" + "=" * 60)
|
||
print("演示完成")
|
||
print("=" * 60)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main()) |