xml解释 OA表单配置文件的权限信息
00.其实可以用AI来写代码的,沟通提示也能写。但自己写也是可以的。费点时间而已。
01.这个是create_table_report.py文件
'''主要目的是查看京华OA一个表单在流程不同节点的填写框不同的读写,编辑,必填权限'''
#000.导入的包
#001.输入目录获取目录中的文件列表的包
import utils.return_dir_file_name as rdf
# #002.导入操作xml的包
# import xml.etree.ElementTree as ET
#003.用于把数据写入Excel的包
import pandas as pd
#004.字符串拆分的包
import re
#006.导入个性化操作xml的包
import utils.xml_handle as xhfrom collections import defaultdict#100.变量
#101.流程所在的目录
flow_dir_path=r'D:\\vb_share\\oa\\表单'
#102.流程报告所在的目录
report_path="D:\\work_report\\表单和流程\\table"
#103.activity元素名列表,因为变量的命名不能用小横杠,所以做了一些有下划线的名
#200.执行的程序
#201.读取目录中的文件名列表
return_file_list=rdf.re_file_name_list(flow_dir_path)
#202.遍历目录中的文件名
for i in return_file_list:#一个excel一个表单print(i)#203.源xml的绝对路径full_name_path=flow_dir_path+'\\'+i# print(full_name_path)#204.只获取文件名only_name=i.split('.')#检查一下文件名# print(only_name)#目标excel的绝对路径,因为不同流程名字不同,所以要用for循环遍历目录excel_path=report_path+'\\'+only_name[0]+'.xlsx'# print(excel_path)#一次性解析 XML 文件tree = xh.ET.parse(full_name_path)root = tree.getroot()#获取分表名sheets_name = xh.get_formset_name(root) #方法已修改,直接接受rootsheet_data = {} #用于缓存每个分表的数据for sheetname in sheets_name:nodes_list = xh.get_node_name(root) #修改接受参数data_dict = defaultdict(dict)for nodes in nodes_list:objects_name = xh.get_formset_object(root,sheetname)#修改接受参数for object_name in objects_name:object_right=xh.get_object_right(root,nodes,object_name)#修改接受参数if object_right:data_dict[object_name][nodes]=object_rightdf = pd.DataFrame.from_dict(data_dict,orient='index')#nicknames是一个字典,字典里面是object_name为key,nickname为value的元素nicknames =xh.get_formset_object(root,sheetname)#在行标题后增加一列,就是Object_name后#这列可以没有列标题,也可以叫别名#别名对应一个行标题df.insert(0, '别名', [nicknames.get(object_name, '') for object_name in df.index]) sheet_data[sheetname]=df# 一次性写入 Excel 文件with pd.ExcelWriter(excel_path) as writer:for sheetname, df in sheet_data.items():df.to_excel(writer, sheet_name=sheetname, header=True, index=True)
02.这是return_dir_file_name.py文件
#这是return_dir_file_name.py文件
import os
#需要读取的路径
path_name = r'D:\Study\myproject\Python_auto_office\auto_check\bat'
def re_file_name_list(path_name):l=[]# item为path_name目录下的所有文件for item in os.listdir(path=path_name):# print(item)l.append(item)return l
# if __name__ == '__main__':
# txt_path='D:\\Study\\myproject\\Python_auto_office\\auto_check\\txt'
# return_file_list=re_file_name_list(txt_path)
# for i in return_file_list:
# print(i)
03.这是xml_handle.py文件
import xml.etree.ElementTree as ET
import re
#返回一个环节属性列表
def get_activity_attribute(xml_path):activity_attribute=[]tree = ET.parse(xml_path)root = tree.getroot() for child in root:# print(child.tag)for little_child in child.findall("activity"):for small_child in little_child:activity_attribute.append(small_child.tag)activity_attribute_tag=activity_attribute[0:21]return activity_attribute_tag
#返回一个连线属性列表
def get_transitions_attribute(xml_path):activity_attribute=[]tree = ET.parse(xml_path)root = tree.getroot() for child in root:for little_child in child.findall("transition"):for small_child in little_child:activity_attribute.append(small_child.tag)activity_attribute_tag=activity_attribute[0:12]# print(activity_attribute_tag)return activity_attribute_tag
#返回一个发送人属性列表
def get_participant_attribute(xml_path):participant_attribute=[]tree = ET.parse(xml_path)root = tree.getroot() # for child in root.findall('.//procinfo/activities/activity/'):for child in root.findall('.//activity/participants'):for little_child in child:for min_child in little_child:# print(min_child.tag,min_child.text) participant_attribute.append(min_child.tag)participant_attribute_tag=participant_attribute[0:3]# print(participant_attribute_tag)return participant_attribute_tag
# 返回流程的基本信息 返回一个字典
def get_flow_base_information(xml_path):base_info_dic={}tree = ET.parse(xml_path)root = tree.getroot() for child in root:base_info_dic[child.tag]=child.textreturn base_info_dic
# 返回环节的基本信息 返回一个字典
def get_flow_activity_information(xml_path):input_activity_excel={}tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)# print(get_activity_attribute_key)i=0for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):for min in small.findall(get_activity_attribute_key[i]):# print(min.tag,min.text)locals()['list_'+str(j)].append(min.text)input_activity_excel[min.tag]=locals()['list_'+str(j)] i=i+1if i == 21:# print(input_activity_excel) return(input_activity_excel)
# 返回连线的基本信息 返回一个字典
def get_flow_transitions_information(xml_path):input_transitions_excel={}tree = ET.parse(xml_path)root = tree.getroot()get_transitions_attribute_key=get_transitions_attribute(xml_path)# print(get_transitions_attribute_key)i=0for j in get_transitions_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("transition"):# print(small.tag,small.text)for min in small.findall(get_transitions_attribute_key[i]):# print(min.tag,min.text)locals()['list_'+str(j)].append(min.text)input_transitions_excel[min.tag]=locals()['list_'+str(j)] i=i+1if i == 12:# print(input_transitions_excel) return(input_transitions_excel)
# 返回发送人的基本信息 返回一个字典
def get_flow_participant_information(xml_path):input_participant_excel={}tree = ET.parse(xml_path)root = tree.getroot()get_participant_attribute_key=get_participant_attribute(xml_path)# print(get_participant_attribute_key)i=0for j in get_participant_attribute_key:locals()['list_'+str(j)] = list()for little in root.findall('.//activity/participants'):for small in little:for min in small.findall(get_participant_attribute_key[i]): ### if min.tag == "name":# print(min.tag,min.text) ##locals()['list_'+str(j)].append(min.text)input_participant_excel[min.tag]=locals()['list_'+str(j)] i=i+1if i == 3:# print(input_participant_excel) return(input_participant_excel) # for small in root:# for min,att in small.findall('activity'),get_activity_attribute_key:# for mirco in min.findall(att):# print(mirco.tag,mirco.text)# 返回环节id的一个列表
def get_activity_id_list(xml_path):activity_id_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):for min in small.findall("id"):# print(min.text)activity_id_list.append(min.text)return activity_id_list# 返回环节name的一个列表
def get_activity_name_list(xml_path):activity_name_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):for min in small.findall("name"):# print(min.text)activity_name_list.append(min.text)return activity_name_list# 返回环节和绑定人员的信息的一个列表
def get_activity_name_and_executer_name_list(xml_path):activity_name_list=[]dictionary_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):#环节的名字for min in small.findall("name"):# print("环节:")# print(min.text)activity_name_list.append(min.text)#绑定的人员信息# print("绑定人员:")for participant in small.findall("participants"):little_element_list=[]object_id_list=[]for p_name in participant.findall("participant"): for a in p_name.findall("name"):#名字print(a.text)for b in p_name.findall("object-id"):#idprint(b.text)# little_element_list.append(a.text)# object_id_list.append(b.text)little_element_list.append(b.text+a.text)# object_id_list.append(b.text)# print(little_element_list)# print("#####")# dictionary = dict(zip(activity_name_list, articipant_name_list))# print(dictionary)dictionary={min.text:little_element_list}# print(dictionary)dictionary_list.append(dictionary)print(dictionary_list)return dictionary_list# 返回环节x座标的一个列表
def get_activity_x_list(xml_path):activity_x_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):for min in small.findall("x"):# print(min.text)activity_x_list.append(min.text)return activity_x_list# 返回环节y座标的一个列表
def get_activity_y_list(xml_path):activity_y_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("activity"):for min in small.findall("y"):# print(min.text)activity_y_list.append(min.text)return activity_y_list# 返回连线id列表
def get_transition_id_list(xml_path):transition_id_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("transition"):for min in small.findall("id"):# print(min.text)transition_id_list.append(min.text)return transition_id_list# 返回连线name列表
def get_transition_name_list(xml_path):transition_name_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("transition"):for min in small.findall("name"):# print(min.text)transition_name_list.append(min.text)return transition_name_list# 返回连线from列表
def get_transition_from_list(xml_path):transition_from_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("transition"):for min in small.findall("from-activity"):# print(min.text)transition_from_list.append(min.text)return transition_from_list# 返回连线to列表
def get_transition_to_list(xml_path):transition_to_list=[]tree = ET.parse(xml_path)root = tree.getroot()get_activity_attribute_key=get_activity_attribute(xml_path)for j in get_activity_attribute_key:locals()['list_'+str(j)] = list()for little in root:for small in little.findall("transition"):for min in small.findall("to-activity"):# print(min.text)transition_to_list.append(min.text)return transition_to_list#############################################################################################################
#表单
#获取表单中附表的名字,反回一个数组
def get_formset_name(root):formset_names=[]# tree = ET.parse(xml_path)# root = tree.getroot()for subforms in root.findall('subforms/'):for subform in subforms:if subform.tag == 'name':# print(subform.text)formset_names.append(subform.text)return formset_names
#通过分表名获取分表中的控件对象,返回对象是一个字典,key为对象名,value为别名
def get_formset_object(root,formset_name):# tree = ET.parse(xml_path)# root = tree.getroot()objects_dict={}for subforms in root.findall('subforms/'):for subform in subforms:if subform.tag == 'name' and subform.text == formset_name :for object in subforms.findall('form-objects/'):# print(object.tag,object.text)nick_name=""object_name=""for element in object:if element.tag == "name":# print(element.tag,element.text)object_name=element.textif element.tag == "binding-data":nick_name=element.textif object_name:objects_dict[object_name]=nick_nameprint(objects_dict)return(objects_dict)
#通过控件对象名获取权限,返回一个字典 key为对象名,value为权限
#把object_right的返回值改为字符串
def get_object_right(root,node_name,object_name):# objects_rights={}right=""for act_right in root.findall('rights/'):# print(act_right[1].tag,act_right[1].text)if act_right[1].text==node_name:for child in act_right[-1]:if child[1].text==object_name:name=child[1].textright=child[2].text# print(name)# print(right)if right == "1":right = "可编辑"if right == "2":right = "只读"# objects_rights[name]=rightreturn right
#返回一个节点列表
def get_node_name(root):nodes_list=[]# tree = ET.parse(xml_path)# root = tree.getroot()for act_right in root.findall('rights/'):nodes_list.append(act_right[1].text)print(nodes_list) return nodes_listdef test_nodes(root):nodes_list=[]for node in root:nodes_list.append(node.tag)print(nodes_list)def find_keyword_paths(root, keyword, path=None):if path is None:path = []result = []for child in root:# 构建当前节点的路径child_path = path + [child.tag]# 检查节点文本是否包含关键字if child.text is not None and keyword in child.text:result.append('/'.join(child_path))# 递归检查子节点result.extend(find_keyword_paths(child, keyword, child_path))return result
#根据控件的id,获取id绑定的别名
def get_nickname(xml_path,id):tree = ET.parse(xml_path)root = tree.getroot()for subform in root.findall('subforms/'):form_objects=subform[6]for FDateField in form_objects:if FDateField[0].text==id:# print(FDateField[0].tag,FDateField[0].text)# print(FDateField[1].tag,FDateField[1].text)print(FDateField[15].tag,FDateField[15].text)return (FDateField[15].tag,FDateField[15].text)#获取表单控件信息
def get_table_object_imformation(xml_path):"rights/act-right/act-name"tree = ET.parse(xml_path)root = tree.getroot()for act_right in root.findall('rights/'):print(act_right[1].tag,act_right[1].text)for child in act_right[-1]:#控件idprint(child[0].tag,child[0].text)#控件名print(child[1].tag,child[1].text)#控件别名nick_name=get_nickname(xml_path,child[0].text)print(nick_name)#控件权限print(child[2].tag,child[2].text)#电脑写的
def get_table_node_imformation(xml_path):tree = ET.parse(xml_path)root = tree.getroot()result_dict={}# 预先获取所有需要的节点,避免重复查找for subform in root.findall('subforms/'):form_objects = subform[6]id_to_nickname = {}for FDateField in form_objects:# 创建ID到别名的映射字典# field_id=FDateField[0]#oject7:别名field_id=FDateField[1]field_nickname=FDateField[15]id_to_nickname[field_id.text] = field_nickname.textresult_dict.update(id_to_nickname)# print(result_dict)for act_right in root.findall('rights/'):#环节名称print(act_right[1].tag,act_right[1].text)for child in act_right[-1]:# id=child[0].textname=child[1].textright=child[2].text#控件名print(name)#控件呢称nickname=result_dict[name]print(nickname)# 控件权限print(right)##8584363#Object46# print(id_to_nickname(id))if __name__ == '__main__':#表单解释
# xml_path="D:\\work_report\\xml转换表单权限的目录\\区府办\\文件呈批表.xml"xml_path="D:\\vb_share\\oa\\流程\\请假流程.xml"flow_dir_path=r'D:\\vb_share\\oa\\流程'# xml_path="D:\vb_share\oa\表单\文件呈批表.xml"formset_name="文件处理表1"object_name="Object46"node_name="收文拟办"tree = ET.parse(xml_path)root = tree.getroot()#只读是2#可编辑是1
# #01获取分表名 ok
# 结果
# ['文件呈批表1', '文件处理表1', '文件处理表2', '转办表', '正文']
# get_formset_name(xml_path)# #02通过表名获取表中的控件对象 返回一个字典对象 key 控件名,value为别名 ok
# get_formset_object(root,formset_name)# get_activity_name_and_executer_name_list(xml_path)# #04.获取节点信息test_nodes(root)# #03通过控件对象名获取权限 key 控件名 value为权限
# get_object_right(xml_path,node_name,object_name)# get_table_object_imformation(xml_path)# get_table_node_imformation(xml_path)# get_nickname(xml_path,'1541807')# xml_path="D:\\work_report\\xml转换流程图目录\\区领导批示收文登记表流程.xml"# result=get_flow_base_information(xml_path)# print(result) # result=get_activity_attribute(xml_path)# print(result)# result=get_flow_activity_information(xml_path)# # result1=get_transitions_attribute(xml_path)# # result=get_participant_attribute(xml_path)# result = get_flow_participant_information(xml_path)# result = get_activity_name_and_executer_name_list(xml_path)# print(result)# for i in result:# for y in i.values():# for j in i:# # print(j)# # print(y)# for q in y:# print(q)# # print("###")# print(result)# activity_id_list=get_activity_id_list(xml_path)# print(activity_id_list)# activity_name_list=get_activity_name_list(xml_path)# print(activity_name_list)# activity_x_list=get_activity_x_list(xml_path)# print(activity_x_list)# activity_y_list=get_activity_y_list(xml_path)# print(activity_y_list)# transition_id_list=get_transition_id_list(xml_path)# print(transition_id_list)# transition_name_list=get_transition_name_list(xml_path)# print(transition_name_list)# transition_from_list=get_transition_from_list(xml_path)# print(transition_from_list)# transition_to_list=get_transition_to_list(xml_path)# print(transition_to_list)
生成的excel效果