python之钉钉审批导出

python之钉钉审批导出

AutoSAR入门到精通系列讲解
将从2019年开始更新关于AutoSAR的知识,从入门到精通,博主xyfx和大家一起进步
雪云飞星
去订阅

钉钉审批导出需要管理员协助配合并且需要参考钉钉开发文档。

1、获取access_token

正常情况下access_token有效期为7200秒

钉钉开发指南:https://open-doc.dingtalk.com/microapp/serverapi2/eev437

appkeyString应用的唯一标识key
appsecretString应用的密钥

 

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
import requests
import json
import datetime
import time
import os
import sys
import csv
import pandas as pd

#获取access_token
def getToken():
appkey = ‘钉钉后台获取,需管理员协助’
appsecret = ‘钉钉后台获取,需管理员协助’
headers = {
‘Content-Type’: ‘application/json’,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36’
}
url = ‘https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s’ % (appkey,appsecret)
req = urllib.request.Request(url,headers= headers)
result = urllib.request.urlopen(req)
access_token = json.loads(result.read())
return access_token[‘access_token’]
accesstoken = getToken()

 

2、批量获取审批实例id

钉钉获取审批实例文档:https://open-doc.dingtalk.com/microapp/serverapi2/hh8lx5

名称类型是否必须示例值描述
process_codeString必须PROC-FF6YR2IQO2-NP3LJ1J0SO4182NKX26K3-3N23J-PB流程模板唯一标识,可在OA管理后台编辑审批表单部分查询
start_timeNumber必须1496678400000开始时间。Unix时间戳
end_timeNumber可选1496678400000结束时间,默认取当前时间。Unix时间戳
sizeNumber可选10分页参数,每页大小,最多传10,默认值:10
cursorNumber可选0分页查询的游标,最开始传0,后续传返回参数中的next_cursor值,默认值:0
userid_listString可选manager1发起人用户id列表,用逗号分隔,最大列表长度:10

1)由于钉钉限制I/O所以每次最多导出10条数据。

2)由于钉钉安全机制,需填写授权公网出口IP。

3、需通管理员获取相关服务的code


  
  
  1. cursor1 = '0'
  2. data = { '钉钉后台获取,需管理员配合', 'start_time': '1538326800', 'size': '10', 'cursor': '1'}
  3. data1 = json.dumps(data).encode(encoding= 'UTF8')
  4. headers1 = { 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'}
  5. url1 = 'https://oapi.dingtalk.com/topapi/processinstance/list?access_token=%s' % (accesstoken)
  6. req = urllib.request.Request(url = url1,headers = headers1,data = data1)
  7. result = urllib.request.urlopen(req)
  8. dic = json.loads(result.read())

4、切片排版,导出格式为.csv


  
  
  1. list1 = (dic.get( 'result', 'none').get( 'list', 'none'))
  2. result = dic[ 'result']
  3. next_cursor = result[ 'next_cursor']
  4. print(next_cursor)
  5. headers = [ '.csv格式自定义', '格式自定义', '格式自定义']
  6. fp = open( 'saas.csv', 'a', encoding= "utf-8_sig")
  7. f_csv = csv.writer(fp)
  8. f_csv.writerow(headers)
  9. for list2 in list1:
  10. title = list2[ 'title']
  11. create_time = list2[ 'create_time']
  12. form_component_values = list2[ 'form_component_values']
  13. name1 = form_component_values[ 1].get( 'name', 'none')
  14. value1 = form_component_values[ 1].get( 'value', 'none')
  15. name2 = form_component_values[ 2].get( 'name', 'none')
  16. value2 = form_component_values[ 2].get( 'value', 'none')
  17. name3 = form_component_values[ 3].get( 'name', 'none')
  18. value3 = form_component_values[ 3].get( 'value', 'none')
  19. name4 = form_component_values[ 3].get( 'name', 'none')
  20. value4 = form_component_values[ 4].get( 'value', 'none')
  21. name5 = form_component_values[ 5].get( 'name', 'none')
  22. value5 = form_component_values[ 5].get( 'value', 'none')
  23. name6 = form_component_values[ 6].get( 'name', 'none')
  24. value6 = form_component_values[ 6].get( 'value', 'none')
  25. rows = [
  26. (title,create_time,value1,value2,value3,value4,value5,value6)
  27. ]
  28. f_csv.writerows(rows)
  29. fp.close()

  

 

转载于:https://www.cnblogs.com/xfyyxz/p/10488038.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值