1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| ''' XX项目凭证自动录入: 1、遍历读取待处理excel,解析具体内容 2、解析银行账号,获取具体的信托项目 3、封装xml报文,生成业务系统凭证 4、修改excel名字,移到已处理excel文件夹 ''' import os import pandas as pd from mssql import connectMSsql,executeSQL,closeConn import requests import html import datetime from loguru import logger import shutil import sqlite3 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header
FileLog = 'D:/workspace/Enfo_Voucher/log/log{}.log'.format(datetime.date.today()) logger.add(FileLog,rotation='500MB',encoding='utf-8',enqueue=True)
def excel_move(path_befor,path_end,excel_name): shutil.move(os.path.join(path_befor,excel_name),os.path.join(path_end,'已完成_'+excel_name))
def read_excel(filepath): excel_list = [] df = pd.read_excel(filepath) for index,row in df.iterrows(): if row[1] == 'XXXX服务股份有限公司': excel_list.append(list(row)) return excel_list
def get_productid(acct_bank): sqlConnectParams = {"database_name": "XXXX", "username": "XXXX", "password": "XXXX", "host": "XXXX", "port": "XXXX", "encoding": "cp936"} conn = connectMSsql(sqlConnectParams) productidSql = r"select b.product_id from NCMIDDLEDB..JQR_ALL_TBANK a left join intrust..tproduct b on b.product_code=a.product_code where a.BANK_ACCT='"+acct_bank+"'" lista = executeSQL(conn,productidSql) accasoaSql = r"select sub_code from intrust..TSUBBANKINFO1021 where ACCT_TYPE = '120801' AND PRODUCT_ID =" + str(lista[0][0]) listb = executeSQL(conn,accasoaSql) closeConn(conn) return lista,listb
def create_voucher(row_excel,product_id,accasoa): now = datetime.datetime.now() timestamp = int(now.timestamp()) PzDate = str(row_excel[0]).replace('-','')[0:8] xmlDetail = ''' <![CDATA[ <root> <row Guid="{}"> <!--guid:【*】业务流水编号(不能重复) --> <Flow> <!--第一笔凭证 --> <Id>1</Id> <!--【*】编号 --> <Productid>{}</Productid> <!--【*】产品ID --> <PzDate>{}</PzDate> <!--【*】记帐日期 --> <BusiDate>{}</BusiDate><!--【*】业务日期 --> <PutMan>cwjqr</PutMan><!--【*】制单人 --> <Description>{}</Description><!--凭证摘要 --> <ListXml> <!--【*】分录1--> <ListId>1</ListId> <!--【*】分录1--> <SubCode>{}</SubCode> <!--【*】明细科目--> <Balance1>{}</Balance1> <!--【*】借方金额--> <Balance2>0</Balance2> <!--【*】贷方金额--> <Description>{}</Description> <!--【*】分录摘要--> </ListXml> <ListXml> <!--【*】分录2--> <ListId>2</ListId> <!--【*】分录2--> <SubCode>224199</SubCode> <!--【*】明细科目--> <Balance1>0</Balance1> <!--【*】借方金额--> <Balance2>{}</Balance2> <!--【*】贷方金额--> <Description>{}</Description> <!--【*】分录摘要--> </ListXml> </Flow> <!--第一笔凭证 end --> </row> </root>]]> '''.format(str(timestamp) , str(product_id) , PzDate , PzDate , row_excel[6] , str(accasoa) , row_excel[3] , row_excel[6] , row_excel[3] , row_excel[6]) xml_data = """ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:intf="http://intf.intrust.enfo.com/"> <soapenv:Header/> <soapenv:Body> <intf:doBizEx> <!--Optional:--> <arg0>ApplyAddPz</arg0> <!--Zero or more repetitions:--> <arg1>Xmlinfo</arg1> <!--Zero or more repetitions:--> <arg2>{}</arg2> </intf:doBizEx> </soapenv:Body> </soapenv:Envelope> """.format(xmlDetail) logger.info(xml_data) url = "http://XXXX:XXXX/CRMServer/services/BaseService?wsdl" headers = { "Content-Type": "text/xml;charset=utf-8", } response = requests.post(url, data=xml_data.encode('utf-8'), headers=headers) if response.status_code == 200: logger.info(html.unescape(response.text)) else: logger.info(f"请求失败,状态码:{response.status_code}") return xml_data,html.unescape(response.text),str(datetime.datetime.today())
def insert_sqlite(contentList): connSqlite = sqlite3.connect(r'D:\sqlite\XXXXDatabase') cursor = connSqlite.cursor() for i in contentList: insertSql = "insert into gl_voucher_detail (sendxml,contentxml,ts) values (?,?,?)" cursor.execute(insertSql,(i[0],i[1],i[2],)) connSqlite.commit() connSqlite.close()
def toManager(): conn = sqlite3.connect(r'D:\sqlite\XXXXDatabase') c = conn.cursor() logger.info('查询是否需要发送邮件') querySql = "select * from gl_voucher_detail where ts like '{}%'".format(datetime.date.today().strftime(r'%Y-%m-%d')) queryResult = c.execute(querySql) if queryResult.fetchone(): logger.info('今日有新增凭证,需发送邮件') subject = '{}-XX系列银行收款凭证处理通知'.format(datetime.date.today().strftime(r'%Y%m%d')) to = 'XXXX@XXXX.cn' contents = '<p><b>今日XX系列银行收款凭证已处理完毕,请登陆业务系统核对。</b></p><span>该邮件为系统自动发送,请勿回复。</span>' smtp = smtplib.SMTP() smtp.connect("mail.XXXX.cn", port=25) smtp.login(user="ai@XXXX.cn", password=r"XXXX") message = MIMEMultipart() message['From'] = Header("ai@XXXX.cn", 'utf-8') message['Subject'] = Header(subject, 'utf-8') message['To'] = Header(to, 'utf-8') message.attach(MIMEText(contents, 'html', 'utf-8')) smtp.sendmail(from_addr="ai@XXXX.cn", to_addrs=to, msg=message.as_string()) smtp.quit() logger.info('邮件发送完成') else: logger.info('今日无需发送邮件') conn.close()
if __name__ == '__main__': logger.info('****准备处理XX项目凭证录入****') path = r'D:/workspace/Enfo_Voucher/excel/待处理excel' path2 = r'D:/workspace/Enfo_Voucher/excel/已处理excel' logger.info('待处理excel路径为:{}'.format(path)) for folder in os.listdir(path): logger.info('处理文件:{}'.format(folder)) acct_bank = folder.split('.')[0][0:21] logger.info('银行账号:{}'.format(acct_bank)) product = get_productid(acct_bank) product_id = product[0][0][0] accasoa_id = product[1][0][0] logger.info('product_id:{} ,accasoa_id:{}'.format(product_id,accasoa_id)) print(os.path.join(path,folder)) content = read_excel(path+'//'+folder) request_list = [] for i in content: logger.info('处理行:{}'.format(i)) request_detail = create_voucher(i,product_id,accasoa_id) request_list.append(request_detail) logger.info('该行处理完毕') logger.info('准备插入sqlite数据库') insert_sqlite(request_list) logger.info('插入sqlite数据库完成') logger.info('文件处理完成,移动至已处理目录') excel_move(path,path2,folder) logger.info('文件:{} ,处理完成'.format(folder)) toManager() logger.info('****XX项目凭证处理完毕****')
|