CMD调用Python编译的exe程序--02

2021-04-30 13:28

阅读:645

标签:false   描述   安全性   __init__   des   位置   href   fill   src   

UIPATH调用Python编译程序exe

好处:
1)code不以可编辑的状态被用户接触,对于不懂反编译的一般用户,可提升一定的代码安全性;
2)不需要用户机器上安装 python环境。
3)可以将能够执行不同任务的脚本编译成同一个exe程序,通过传入不同参数实现不同任务。

1).生成并调用单任务 exe程序:
详见之前的博客:https://blog.csdn.net/qq_24937551/article/details/105517535
  1. 准备单任务python脚本 2. 将仅执行单任务的脚本打包成exe 3. UIPATH调用exe
2).生成并调用多任务 exe程序:
  1. 准备多任务python脚本 2. 将可执行多任务的脚本打包成exe 3. UIPATH调用exe
  2. UIPATH 调用 exe或者python脚本,实际上是使用vb.net来调用cmd命令行来执行exe或脚本,所以难点在于参数的传递和解析,尤其是当脚本中的函数需要多种类型的参数,比如位置参数,关键字参数等。
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
import sys,getopt  # 用来解析参数的两个库

class HandleData():
    def __init__(self,process_name,log_path=‘‘,excel_path=‘‘,new_excel_path=‘‘):
        self.process_name=process_name
        self.log_path=log_path
        self.excel_path=excel_path
        self.new_excel_path=new_excel_path

    def deal_with_data(self):
        file_obj=open(self.excel_path)
        df=pd.read_csv(file_obj)
        # df=pd.read_csv(self.excel_path)
        df=df.reindex(columns=[‘CNUM‘,‘COMPANY‘,‘C_col‘,‘D_col‘,‘E_col‘,‘F_col‘,‘G_col‘,‘H_col‘],fill_value=None)
        df.rename(columns={‘COMPANY‘:‘Company_New‘}, inplace = True)
        df=df.dropna(axis=0,how=‘all‘)
        df[‘CNUM‘] = df[‘CNUM‘].astype(‘int32‘)
        df = df.drop_duplicates(subset=[‘CNUM‘, ‘Company_New‘], keep=‘first‘)

        df.to_csv(self.new_excel_path,index=False,encoding=‘GBK‘)
        file_obj.close()

    def writeLog(self):
        with open(self.log_path,"a") as logfile:
            logfile.write("\nthat‘s a test log message")
        
    def writeEventLog(self):
        with open(r"C:\Users\12078\Desktop\UIPATH_test\0419\EventLogs_Bot1.txt","a") as logfile:
            logfile.write("\nno function found" + self.process_name)
         
    def mainprocess(self):
        if self.process_name=="deal_with_data":
           HandleData.deal_with_data(self)
        elif self.process_name=="writeLog":
           HandleData.writeLog(self)
        else:
           HandleData.writeEventLog(self)

if __name__ == "__main__":
    process_name=""
    log_path = ""
    excel_path = ""
    new_excel_path = ""
    argv=sys.argv[1:]
    try:
        opts,args = getopt.getopt(argv,"h",["process_name=","log_path=","excel_path=","new_excel_path="])
    except getopt.GetoptError:
        print(‘cnum_company_data0418.py --process_name processname str --log_path logpath str --excel_path datafilepath str --new_excel_path outputfilepath str‘)
        sys.exit(2)
    for opt, arg in opts:
        if opt == "-h":
            print(‘cnum_company_data0418.py --process_name processname str --log_path logpath str --excel_path datafilepath str --new_excel_path outputfilepath str‘)
            sys.exit()
        elif opt == "--process_name":
            process_name = arg
        elif opt == "--log_path":
            log_path = arg
        elif opt =="--excel_path":
            excel_path = arg
        elif opt =="--new_excel_path":
            new_excel_path = arg
    # print(process_name + "--"+log_path+"--"+excel_path+"--"+new_excel_path+"")
    handle=HandleData(process_name,log_path,excel_path,new_excel_path)
    handle.mainprocess()

cmd执行python脚本测试效果:
  1. 测试生成log message的功能:
    C:\Users\12078\Desktop\UIPATH_test\0419>python cnum_company_data0419.py --process_name=writeLog --log_path=C:\Users\12078\Desktop\UIPATH_test\0419\mylog.txt

  2. 测试处理excel/csv数据的功能:
    C:\Users\12078\Desktop\UIPATH_test\0419>python cnum_company_data0419.py --process_name=deal_with_data --excel_path=C:\Users\12078\Desktop\UIPATH_test\0419\CNUM_COMPANY.csv --new_excel_path=C:\Users\12078\Desktop\UIPATH_test\0419\out.csv

经过测试,脚本没有问题

以上程序也可以这样实现:
# 知识点:面向对象,反射,元组解包,不定长参数的函数
import pandas as pd
from pandas import DataFrame,Series

class HandleData():

    def deal_with_data(self,paths):
        excel_path,new_excel_path=paths
        file_obj=open(excel_path)
        df=pd.read_csv(file_obj)
        df=df.reindex(columns=[‘CNUM‘,‘COMPANY‘,‘C_col‘,‘D_col‘,‘E_col‘,‘F_col‘,‘G_col‘,‘H_col‘],fill_value=None)
        df.rename(columns={‘COMPANY‘:‘Company_New‘}, inplace = True)
        df=df.dropna(axis=0,how=‘all‘)
        df[‘CNUM‘] = df[‘CNUM‘].astype(‘int32‘)
        df = df.drop_duplicates(subset=[‘CNUM‘, ‘Company_New‘], keep=‘first‘)

        df.to_csv(new_excel_path,index=False,encoding=‘GBK‘)
        file_obj.close()

    def writeLog(self,log_path):
        log_path=log_path[0]
        with open(log_path,"a") as logfile:
            logfile.write("\nthat‘s a test log message")
        
    def writeEventLog(self,p):
        process_name,content=p
        with open(r"C:\Users\12078\Desktop\uibot\pytest_mutiply.txt","a") as logfile:
            logfile.write("\nno function found" + process_name+"--"+content)
         
        
def main(f,*args):
    h=HandleData()
    if hasattr(h,f):
        getattr(h,f)(args)
    
    
if __name__ == "__main__":
    main(‘writeEventLog‘,‘fuc_test‘,‘ttesthahah‘)
cmd执行exe编译任务:

C:\Users\12078\Desktop\UIPATH_test\0419>pyinstaller cnum_company_data0419.py

UIPATH调用 exe执行多任务:

1.总体框架:技术图片
技术图片

2.第1次调用exe完成第一个任务:技术图片
技术图片

3.第2次调用 exe完成第二个任务:

技术图片
技术图片

4.执行结果:

技术图片
技术图片

说明

1.Author: Collin_PXY ; wechat: pxy123abc

CMD调用Python编译的exe程序--02

标签:false   描述   安全性   __init__   des   位置   href   fill   src   

原文地址:https://www.cnblogs.com/Collin-pxy/p/13227861.html


评论


亲,登录后才可以留言!