【影刀RPA_python_发票明细拆分】

影刀RPA_python_发票明细拆分

# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot
from xbot import print, sleep
from .import package
from .package import variables as glv
import pandas as pd
import xbot_visual

def main(args):
    pass

def insert_into():
    global number
    global remarkColumnlist
    global rowNumList
    global totalAmount

    number=number+1
    # print(f"number是{number}")
    # print(rowNumList)
    for rowNum in rowNumList:
        remarkColumnlist.append("开第"+str(number)+"张票")
    rowNumList =[]
    totalAmount =0

def split_invoice_details(maxAmount,filePath,sheetName,dataSourceList,write_row_num,write_column_name):
    global number
    global remarkColumnlist
    global rowNumList
    global totalAmount

    df1 = pd.DataFrame(dataSourceList[1:], columns=dataSourceList[0])
    #新增行号列
    df1['行号'] = list(range(2, len(df1)+2)) 
    # #重置df1的索引
    df1= df1.set_index(['开票主体', '对方公司'])
    result_dict = {}  
    for i, row in zip(df1.index.tolist(), df1.to_dict('records')):
        # print(i)
        # print(row)
        if i in result_dict:
            result_dict[i].append(row)
        else:
            result_dict[i] = [row]
    # print(result_dict)

    #循环字典
    number=0
    remarkColumnlist=[]
    for key,valueList in result_dict.items():
        n=0
        totalAmount=0
        rowNumList=[]
        # print(valueList)
        for value in valueList:
            n = n+1
            totalAmount=totalAmount+value["金额"]
            if totalAmount <= float(maxAmount):
                rowNumList.append(value["行号"])
                if n== len(valueList):
                    insert_into()
            else:
                #把之前的开了
                insert_into()
                #重置一下变量
                rowNumList=[value["行号"]]
                totalAmount=value["金额"]
                # print(f"走这里,此时行号{rowNumList}")
                if n== len(valueList):
                    insert_into()
    # print(remarkColumnlist)
    df1["备注"]=remarkColumnlist

    df1_last_two_columns = df1.iloc[:, -2:]  # 使用iloc选择最后两列  
    header = df1_last_two_columns.columns.tolist()  # 获取表头  
    data = df1_last_two_columns.values.tolist()  # 将数据转换为列表  
    # 将表头和数据合并为一个二维列表
    last_two_columns_list = [header] + data
    # print(last_two_columns_list)
    #写入内容到excel
    excel_instance = xbot_visual.excel.launch(launch_way="open", driver_way="auto_check", open_filename=filePath, save_filename="", isvisible=True, ignoreformula=False, password=None, write_password=None, update_links=False)
    xbot_visual.excel.write_data_to_workbook(workbook=excel_instance, write_range="area", write_way="append", write_column_way="override", row_num=write_row_num, column_name=write_column_name, begin_row_num="0", begin_column_name="", content=last_two_columns_list, sheet_name=sheetName, write_as_text_cols=None)
    xbot_visual.excel.save(excel_instance=excel_instance, save_way="save", filename=None, overwrite_file=True, is_close_after_save=False)

    #输出字典
    df1=df1.reset_index()
    df1= df1.set_index(['备注'])
    return_dict = df1.groupby(['备注']).apply(lambda x: x.to_dict('split')['data']).to_dict()
    return return_dict

你可能感兴趣的:(rpa,python,人工智能)