excel转换成XML--3.15

1.excel转换成XML

2.寄存器格式

 

 

//-----------------3.15

# -*- coding:utf-8 -*-
# @Author: Alex

import sys
import os
import re

import xlrd
import xml.dom.minidom

# ======================================================================================================================
# ============================================================================
# 函数     :   处理Excel 第5行数据
#         :   返回 Alex_temp_size, Alex_temp_L.group()
# ============================================================================
def Match_string_5L(old):
    try:
        #----------------------------------------
        Alex_temp_A = re.search(r'.+]', old, re.M | re.S)  # \s*
        #print("1-1-->:", Alex_temp_A.span(), old, Alex_temp_A.group())  # 具体位置
        Alex_temp_H = re.search(r'\d+', Alex_temp_A.group(), re.M | re.S)
        #print('1-3-->:', Alex_temp_H.group())
        Alex_temp_B = re.findall(r'[^\D+]', Alex_temp_A.group(), re.S)
        Alex_temp_B = ''.join(Alex_temp_B)
        Alex_temp_L = re.search(r'.$', Alex_temp_B)
        #print('1-4-->:', Alex_temp_L.group())
        Alex_temp_size = ord(Alex_temp_H.group()) - ord(Alex_temp_L.group()) + 1
        #print('1-5-->:', Alex_temp_size)
        #----------------------------------------
        return Alex_temp_size, Alex_temp_L.group()
    except:
        print("Match_string_5L")

# ======================================================================================================================
# ============================================================================
# 函数     :  处理Excel 第4行数据
#         :  返回 content type
# ============================================================================
def Match_String_4L(old):
    try:
        new_temp_B = re.search(r'RW|RO|SC|RC', old, re.M | re.S)  # \s*
        if new_temp_B.group() == 'RO':
            new_temp_C = new_temp_B.group().replace('RO', 'read-only')
            return new_temp_C
        elif new_temp_B.group() == 'RW':
            new_temp_D = new_temp_B.group().replace('RW', 'read-write')
            return new_temp_D
        elif new_temp_B.group() == 'SC':
            new_temp_E = new_temp_B.group().replace('SC', 'self-clear')
            return new_temp_E
        elif new_temp_B.group() == 'RC':
            new_temp_F = new_temp_B.group().replace('RC', 'clear-on-read')
            return new_temp_F
        else:
            print("")
    except Exception as alex:
        print(alex)

# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Match_Map_A1  -- A1
# Explain :   

# ============================================================================
'''
def Match_Map_A1(B_P1, B_P2, B_P3):
    Document   = B_P1
    Node_M3_A0 = B_P2
    Line_Value = B_P3
    # ----------------------------------------------------------------------------
    Node_REG_M4_A0 = Document.createElement('spirit:addressBlock')
    Node_M3_A0.appendChild(Node_REG_M4_A0)

    # 第四层第一元素
    Node_Four_Level_Alex_M41_A1 = Document.createElement('spirit:name')
    Node_Four_Level_Text_Alex_M41_A1 = Document.createTextNode(Line_Value)
    Node_Four_Level_Alex_M41_A1.appendChild(Node_Four_Level_Text_Alex_M41_A1)
    Node_REG_M4_A0.appendChild(Node_Four_Level_Alex_M41_A1)

    # 第四层第二元素
    Node_Four_Level_Alex_M42_A1 = Document.createElement('spirit:baseAddress')
    Node_Four_Level_Text_Alex_M42_A1 = Document.createTextNode('0x000')
    Node_Four_Level_Alex_M42_A1.appendChild(Node_Four_Level_Text_Alex_M42_A1)
    Node_REG_M4_A0.appendChild(Node_Four_Level_Alex_M42_A1)

    # 第四层第三元素
    Node_Four_Level_Alex_M43_A1 = Document.createElement('spirit:range')
    Node_Four_Level_Text_Alex_M43_A1 = Document.createTextNode('0x40')
    Node_Four_Level_Alex_M43_A1.appendChild(Node_Four_Level_Text_Alex_M43_A1)
    Node_REG_M4_A0.appendChild(Node_Four_Level_Alex_M43_A1)

    # 第四层第四元素
    Node_Four_Level_Alex_M44_A1 = Document.createElement('spirit:width')
    Node_Four_Level_Text_Alex_M44_A1 = Document.createTextNode('8')
    Node_Four_Level_Alex_M44_A1.appendChild(Node_Four_Level_Text_Alex_M44_A1)
    Node_REG_M4_A0.appendChild(Node_Four_Level_Alex_M44_A1)

    return (Document, Node_REG_M4_A0)
# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Map_Block_A2 -- A2
# ============================================================================
'''

def Map_Block_A2(B_R_P1, B_R_P2, B_R_P3, B_R_P4, B_R_P5, B_R_P6):
    Document       = B_R_P1
    Root_Table     = B_R_P2
    Nrow_Count     = B_R_P3
    Node_REG_M4_A0 = B_R_P4
    Access_4L      = B_R_P5
    Block_Value    = B_R_P6
    # ----------------------------------------------------------------------------
    Node_REG_M5_A0 = Document.createElement('spirit:register')
    Node_REG_M4_A0.appendChild(Node_REG_M5_A0)

    Node_REG_M5_A2_E = Document.createElement('spirit:name')
    # 第1列
    Node_REG_M5_A2_E.appendChild(Document.createTextNode(Block_Value + '_' + Root_Table.cell(Nrow_Count, 1).value))
    Node_REG_M5_A0.appendChild(Node_REG_M5_A2_E)

    Node_Manager_Register_M5_A4_E = Document.createElement('spirit:description')
    Node_Manager_Register_M5_A4_E.appendChild(Document.createTextNode('register'))
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M5_A4_E)

    Node_Manager_Register_M5_A4_E_Add1 = Document.createElement('spirit:addressOffset')
    # 第2列
    Node_Manager_Register_M5_A4_E_Add1.appendChild(Document.createTextNode(Root_Table.cell(Nrow_Count, 2).value))
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M5_A4_E_Add1)

    Node_Manager_Register_M5_A5_E = Document.createElement('spirit:size')
    Node_Manager_Register_M5_A5_E.appendChild(Document.createTextNode('8'))  # 获取方式待修改
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M5_A5_E)
    # ----------------------------------------------------------------------------
    Node_REG_M5_A6_E = Document.createElement('spirit:access')   # TODO
    Node_REG_M5_A6_E.appendChild(Document.createTextNode(str(Access_4L)))  # 带修改位置
    Node_REG_M5_A0.appendChild(Node_REG_M5_A6_E)

    Node_Manager_Register_M5_A8_E = Document.createElement('spirit:regbreak')
    Node_Manager_Register_M5_A8_E.appendChild(Document.createTextNode('1'))
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M5_A8_E)

    Node_Manager_Register_M5_A9_E = Document.createElement('spirit:regindex')
    Node_Manager_Register_M5_A9_E.appendChild(Document.createTextNode('0'))
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M5_A9_E)

    # 第五层第10元素 & 虚拟第六层
    Node_Manager_Register_M6_A0_E = Document.createElement('spirit:reset')
    Node_REG_M5_A0.appendChild(Node_Manager_Register_M6_A0_E)
    # 第六层第二节点
    Node_Manager_Register_M6_A2_E = Document.createElement('spirit:value')
    # 第3列
    Node_Manager_Register_M6_A2_E.appendChild(Document.createTextNode(Root_Table.cell(Nrow_Count, 3).value))
    Node_Manager_Register_M6_A0_E.appendChild(Node_Manager_Register_M6_A2_E)

    return (Node_REG_M5_A0, Nrow_Count, Node_REG_M5_A6_E)
# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Map_Block_A3 A3
# ============================================================================
'''

def Map_Block_A3(B_R_P1, B_R_P3, B_R_P4, B_R_P5, B_R_P6, B_R_P7, B_R_P8):
    Document       = B_R_P1
    T_Nrow_Count   = B_R_P3
    Access_4A      = B_R_P4
    New_Content_5A = B_R_P5
    New_Size_5A    = B_R_P6
    New_Offset_5A  = B_R_P7
    Node_REG_M5_A0 = B_R_P8
    # ----------------------------------------------------------------------------
    # 第五层第11元素 & 虚拟第六层 S:表示同级虚拟节点
    Node_REG_M6_S2_A0 = Document.createElement('spirit:field')
    Node_REG_M5_A0.appendChild(Node_REG_M6_S2_A0)

    # 第六层S2第三节点
    Node_REG_M6_S2_A3_E = Document.createElement('spirit:name')
    Node_REG_M6_S2_A3_E.appendChild(Document.createTextNode(New_Content_5A))
    Node_REG_M6_S2_A0.appendChild(Node_REG_M6_S2_A3_E)

    # 第六层S2第节点
    Node_Manager_Register_M6_S2_A4_E = Document.createElement('spirit:bitOffset')
    Node_Manager_Register_M6_S2_A4_E.appendChild(Document.createTextNode(New_Offset_5A))
    Node_REG_M6_S2_A0.appendChild(Node_Manager_Register_M6_S2_A4_E)

    Node_Manager_Register_M6_S2_A5_E = Document.createElement('spirit:bitWidth')
    Node_Manager_Register_M6_S2_A5_E.appendChild(Document.createTextNode(str(New_Size_5A)))
    Node_REG_M6_S2_A0.appendChild(Node_Manager_Register_M6_S2_A5_E)

    if Access_4A == "read-only":
        Node_Manager_Register_M6_S2_A8_E = Document.createElement('spirit:volatile')
        Node_Manager_Register_M6_S2_A8_E.appendChild(Document.createTextNode('true'))
        Node_REG_M6_S2_A0.appendChild(Node_Manager_Register_M6_S2_A8_E)

    Node_Manager_Register_M6_S2_A6_E = Document.createElement('spirit:access')
    Node_Manager_Register_M6_S2_A6_E.appendChild(Document.createTextNode(str(Access_4A)))
    Node_REG_M6_S2_A0.appendChild(Node_Manager_Register_M6_S2_A6_E)

    if Access_4A == "self-clear":
        #
        Node_Manager_Register_M7_S3_A0_E = Document.createElement('spirit:parameters')
        Node_REG_M6_S2_A0.appendChild(Node_Manager_Register_M7_S3_A0_E)

        Node_REG_M8_S1_A0_E = Document.createElement('spirit:parameter')
        Node_Manager_Register_M7_S3_A0_E.appendChild(Node_REG_M8_S1_A0_E)

        Node_REG_M8_S1_A2_E = Document.createElement('spirit:name')
        Node_REG_M8_S1_A2_E.appendChild(Document.createTextNode('access-param'))
        Node_REG_M8_S1_A0_E.appendChild(Node_REG_M8_S1_A2_E)

        Node_REG_M8_S1_A3_E = Document.createElement('spirit:value')
        Node_REG_M8_S1_A3_E.appendChild(Document.createTextNode('self-clear'))
        Node_REG_M8_S1_A0_E.appendChild(Node_REG_M8_S1_A3_E)

    return Node_REG_M5_A0, T_Nrow_Count
# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Map_Block_Register A4
# ============================================================================
'''

def Map_Block_A4( B_R_P1, B_R_P2, B_R_P3, B_R_P4, B_R_P5,  B_R_P6, B_R_P7 ):
    Document       = B_R_P1
    T_Nrow_Count   = B_R_P2
    Content_6L     = B_R_P3
    Offset_5L      = B_R_P4
    Size_5L        = B_R_P5
    Access_5L_B    = B_R_P6
    Node_REG_M5_A0 = B_R_P7
    # ----------------------------------------------------------------------------
    # 第五层第11元素 & 虚拟第六层 S:表示同级虚拟节点
    Node_REG_M6_S2_A1 = Document.createElement('spirit:field')
    Node_REG_M5_A0.appendChild(Node_REG_M6_S2_A1)

    # 第六层S2第三节点
    Node_REG_M6_S2_A3 = Document.createElement('spirit:name')
    Node_REG_M6_S2_A3.appendChild(Document.createTextNode(Content_6L))
    Node_REG_M6_S2_A1.appendChild(Node_REG_M6_S2_A3)

    # 第六层S2第节点
    # ------------------------------------
    Node_REG_M6_S2_A4 = Document.createElement('spirit:bitOffset')
    Node_REG_M6_S2_A4.appendChild(Document.createTextNode(Offset_5L))
    Node_REG_M6_S2_A1.appendChild(Node_REG_M6_S2_A4)

    Node_REG_M6_S2_A5 = Document.createElement('spirit:bitWidth')
    Node_REG_M6_S2_A5.appendChild(Document.createTextNode(str(Size_5L)))
    Node_REG_M6_S2_A1.appendChild(Node_REG_M6_S2_A5)

    if Access_5L_B == "read-only":
        Node_REG_M6_S2_A8 = Document.createElement('spirit:volatile')
        Node_REG_M6_S2_A8.appendChild(Document.createTextNode('true'))
        Node_REG_M6_S2_A1.appendChild(Node_REG_M6_S2_A8)

    Node_REG_M6_S2_A6 = Document.createElement('spirit:access')
    Node_REG_M6_S2_A6.appendChild(Document.createTextNode(str(Access_5L_B)))
    Node_REG_M6_S2_A1.appendChild(Node_REG_M6_S2_A6)

    if Access_5L_B == "self-clear":
        Node_REG_M7_S3_A2 = Document.createElement('spirit:parameters')
        Node_REG_M6_S2_A1.appendChild(Node_REG_M7_S3_A2)

        Node_REG_M8_S1_A0_E = Document.createElement('spirit:parameter')
        Node_REG_M7_S3_A2.appendChild(Node_REG_M8_S1_A0_E)

        Node_REG_M8_S1_A2_E = Document.createElement('spirit:name')
        Node_REG_M8_S1_A2_E.appendChild(Document.createTextNode('access-param'))
        Node_REG_M8_S1_A0_E.appendChild(Node_REG_M8_S1_A2_E)

        Node_REG_M8_S1_A3_E = Document.createElement('spirit:value')
        Node_REG_M8_S1_A3_E.appendChild(Document.createTextNode('self-clear'))
        Node_REG_M8_S1_A0_E.appendChild(Node_REG_M8_S1_A3_E)

    return Node_REG_M5_A0, T_Nrow_Count
# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Map_Block  5(主) name合并版本
# Explain :  
#         :  
# ============================================================================
'''

def Map_AddrBlock_5E(Map_AB_P1, Map_AB_P2, Map_AB_P3, Map_AB_P4,Map_AB_P5, Map_AB_P6) :
    global Merged_Count, Rownames_Value, Node_REG_M5_A0, Access_4L_Flag, Access_4A
    Document            = Map_AB_P1
    Root_Table          = Map_AB_P2
    Node_Manager_M3     = Map_AB_P3
    Name_List_A         = Map_AB_P4       # 列表中每个值
    Name_List_B         = (Map_AB_P5)     # 起始位置
    Excel_Nrow_First_C  = (Map_AB_P6)     # 个数
    # -----------------------------------------------------------------------
    Excel_Nrow_First_D      = Name_List_B + Excel_Nrow_First_C
    Document, Node_REG_M4_A0 = Match_Map_A1( Document, Node_Manager_M3, Name_List_A )
    # ----------------------------------------------------------------------------

    for T_Nrow_A_C in range(Name_List_B , Excel_Nrow_First_D ):
        Rownames_Value = Root_Table.row_values(T_Nrow_A_C)  # 表格中每行的值,list
        # ----------------------------------------------------------------------------
        if Rownames_Value[2] == '':
            Merged_Count += 1
            Access_4L_Flag = 0
        else:
            Merged_Count = 0
        # ----------------------------------------------------------------------------

        # 函数处理列数据
        New_Size_5A, New_Offset_5A = Match_string_5L(Root_Table.cell(T_Nrow_A_C, 5).value)  # 第5列
        New_Content_5A = Root_Table.cell(T_Nrow_A_C, 6).value                                   # 第6列

        # 处理 第4列
        if Rownames_Value[4] != '':
            Access_4A = Match_String_4L(Root_Table.cell(T_Nrow_A_C, 4).value)

        # ----------------------------------------------------------------------------
        if Merged_Count == 0:
            Node_REG_M5_A0, T_Nrow_Count, Node_REG_M5_A6_E = Map_Block_A2(Document, Root_Table, T_Nrow_A_C, Node_REG_M4_A0, Access_4A, Name_List_A )
            Node_REG_M5_A0, T_Nrow_Count = Map_Block_A3( Document, T_Nrow_A_C, Access_4A, New_Content_5A,New_Size_5A, New_Offset_5A, Node_REG_M5_A0)
        else:
            Merged_Count   -= 1
            T_Nrow_Count_B  = T_Nrow_A_C - Merged_Count + 1
            New_Size_5A, New_Offset_7_B = Match_string_5L(Root_Table.cell(T_Nrow_Count_B - 1, 5).value)  # 第5列
            New_Content_5A = Root_Table.cell(T_Nrow_A_C, 6).value  # 第6列
            Node_REG_M5_A0, T_Nrow_Count = Map_Block_A4(Document,T_Nrow_A_C,New_Content_5A,New_Offset_7_B,New_Size_5A,Access_4A,Node_REG_M5_A0)
            # ----------------------------------------------------------------------------
    return Excel_Nrow_First_C
    # ----------------------------------------------------------------------------
# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Mapping
# ============================================================================
'''

def Match_Mapping(Map_G_P1, Map_G_P2, Map_G_P3, Map_G_P4):
    Root_Table = Map_G_P1
    Document = Map_G_P2
    Node_M_A7_A0 = Map_G_P3
    Transverse_Nrows = Map_G_P4
    # ----------------------------------------------------------------------------
    # 第一层第六元素  -虚拟 第二层  S16 = M
    Node_M_3RD_M3_A0 = Document.createElement('spirit:memoryMaps')
    Node_M_A7_A0.appendChild(Node_M_3RD_M3_A0)
    # 第三层第二元素
    Node_M_3RD_M30 = Document.createElement('spirit:memoryMap')
    Node_M_3RD_M3_A0.appendChild(Node_M_3RD_M30)
    # 第三层第二元素
    Node_Three_Level_Alex_M32 = Document.createElement('spirit:name')
    Node_Three_Level_Alex_M32.appendChild(Document.createTextNode('mmap'))
    Node_M_3RD_M30.appendChild(Node_Three_Level_Alex_M32)
    # ----------------------------------------------------------------------------

    # ----------------------------------------------------------------------------
    Alex_Name_List_Subset  = []
    Alex_Name_List_Count_A = []
    Name_List_B = []

    #merge = Root_Table.merged_cells  # 获取合并的单元格
    #print('merge', merge)
    # -----------------------------------------------------------------------------
    Alex_Start = 1
    for T_Nrow_Count in range(Alex_Start, Transverse_Nrows):
        if Root_Table.cell(T_Nrow_Count, 0).value != '' :
            Alex_Name_List_Count_A.append ( T_Nrow_Count)
            Alex_Name_List_Subset.append( Root_Table.cell(T_Nrow_Count, 0).value )

    Alex_Name_List_Count_A.append( T_Nrow_Count + 1 )  # +1 目的是为了最后一列的统计
    for count in range(0,len(Alex_Name_List_Count_A) - 1):
        Name_List_B.append( Alex_Name_List_Count_A[count + 1] - Alex_Name_List_Count_A[count])

    #print('Block 内行数-->: ', Name_List_B) # 记录每个name的个数
    #print('Block 名称  -->: ',Alex_Name_List_Subset,len(Alex_Name_List_Subset))  # 记录name唯一名称

    Alex_Nrow_Count_A = 1  # 第一个Block 起始位置
    # 核心代码 Alex
    for SN in range(len(Alex_Name_List_Subset)):
        #print('遍历-Name -->:', Alex_Name_List_Subset[SN], Name_List_B[SN])
        Excel_Nrow_First_C = Map_AddrBlock_5E(Document, Root_Table, Node_M_3RD_M30, Alex_Name_List_Subset[SN],  Alex_Nrow_Count_A, Name_List_B[SN])
        Alex_Nrow_Count_A = Alex_Nrow_Count_A + Excel_Nrow_First_C

    Node_Three_Level_Alex_M33 = Document.createElement('spirit:addressUnitBits')
    Node_Three_Level_Alex_M33.appendChild(Document.createTextNode('8'))
    Node_M_3RD_M30.appendChild(Node_Three_Level_Alex_M33)
    # -----------------------------------------------------------------------------

# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   Parameters 
# ============================================================================
'''

def Match_Parameters(Document, Node_M_A7_A0):
    # 第一级节点第七元素-第二层  S17 = P
    Node_M_2ND_P2_A0 = Document.createElement('spirit:parameters')
    Node_M_A7_A0.appendChild(Node_M_2ND_P2_A0)

    # 第二层第二元素 & 虚拟第三层
    Node_Manager_Three_Alex_P3 = Document.createElement('spirit:parameter')
    Node_M_2ND_P2_A0.appendChild(Node_Manager_Three_Alex_P3)

    # 第三层第一元素
    Node_Three_Level_Alex_P31 = Document.createElement('spirit:name')
    Node_Three_Level_Text_Alex_P31 = Document.createTextNode('AddressRange')  # 元素内容写入
    Node_Three_Level_Alex_P31.appendChild(Node_Three_Level_Text_Alex_P31)
    Node_Manager_Three_Alex_P3.appendChild(Node_Three_Level_Alex_P31)
    Node_M_2ND_P2_A0.appendChild(Node_Manager_Three_Alex_P3)

    # 第三层第二元素
    Node_Three_Level_Alex_P32 = Document.createElement('spirit:value')
    Node_Three_Level_Text_Alex_P32 = Document.createTextNode('0xFF')
    Node_Three_Level_Alex_P32.appendChild(Node_Three_Level_Text_Alex_P32)
    Node_Manager_Three_Alex_P3.appendChild(Node_Three_Level_Alex_P32)
    Node_M_2ND_P2_A0.appendChild(Node_Manager_Three_Alex_P3)

# ======================================================================================================================


# ======================================================================================================================
'''
# ============================================================================
# 函数     :   创建XML 多级生成
# Explain :  
#         :  
# ============================================================================
'''

def Match_XML_Head():
    # ------------------------------------------------------------------------
    # 创建dom文档对象
    Document = xml.dom.minidom.Document()  # print("--> 创建dom文档对象 ",Document )
    # 创建根元素
    Root_Info = Document.createElement('spirit:component')  # print("--> 创建根元素 ", Root_Info )

    Root_Info.setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
    Root_Info.setAttribute('xsi:schemaLocation', 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1685-2009/tools/regman/lib/XMLSchema/SPIRIT/1685-2009/index.xsd')
    Root_Info.setAttribute('xmlns:com.magillem.mrv.model', 'http:///com/magillem/mrv/model.ecore')
    Root_Info.setAttribute('xmlns:spirit', 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1685-2009')
    Document.appendChild(Root_Info)  # 将根元素添加到文档中区

    # ------------------------------------------------------------------------
    # 第一级节点第一元素
    Node_1ST_A1 = Document.createElement('spirit:vendor')
    Node_1ST_A1.appendChild(Document.createTextNode('cadence'))
    Root_Info.appendChild(Node_1ST_A1)

    # 第一级节点第二元素
    Node_1ST_A2 = Document.createElement('spirit:library')
    Node_1ST_A2.appendChild(Document.createTextNode('Liszt_ddi'))
    Root_Info.appendChild(Node_1ST_A2)

    # 第一级节点第三元素
    Node_1ST_A3 = Document.createElement('spirit:name')
    Node_1ST_A3.appendChild(Document.createTextNode('Liszt'))
    Root_Info.appendChild(Node_1ST_A3)

    # 第一级节点第四元素
    Node_1ST_A4 = Document.createElement('spirit:version')
    Node_1ST_A4.appendChild(Document.createTextNode('1.0'))
    Root_Info.appendChild(Node_1ST_A4)

    # ------------------------------------------------------------------------
    return Document, Root_Info
    # ------------------------------------------------------------------------


# ======================================================================================================================

'''
# ============================================================================
# 函数     :    实现Excel 转换 xml  -- main
# Explain :    
#         :  
# ============================================================================
'''

def Translate_Excel_To_Xml(excel_absolute_path, name, generate_xml_dir, by_index=1):
    # 解析excel文件
    Data_Excel = xlrd.open_workbook(excel_absolute_path)
    # 获取需要的工作表  by_index=1  sheet2
    Root_Table = Data_Excel.sheets()[by_index]
    # ------------------------------------------------------------------------
    global Transverse_Nrows_Last
    Transverse_Nrows = Root_Table.nrows  # 行数 Transverse
    #Vertical_Ncols = Root_Table.ncols  # 列数 vertical
    # -------------------------------------------------------------------------
    # Head 区域
    Document, Root_Info = Match_XML_Head()
    # Mapping 区域
    Match_Mapping(Root_Table, Document, Root_Info, Transverse_Nrows)
    # Parameters 区域
    Match_Parameters(Document, Root_Info)

    # ----------------------------------------------------------------------------
    # 要生成的xml文件名
    generate_xml_name = name.strip().split('.')[0] + '.xml'
    # 要生成的xml文件到某个目录的绝对路径
    geneate_xml_dir = os.path.join(generate_xml_dir, generate_xml_name)
    f = open(geneate_xml_dir, 'w')
    # Document.writexml(f) # 可以使生成xml有好看的格式,要是不需要,可以使用上一行的代码
    f.write(Document.toprettyxml())
    f.close()
    # ----------------------------------------------------------------------------

# ======================================================================================================================

# ======================================================================================================================
'''
Alex
功能说明:
1.处理Excel表转换成特定格式Xml
2.自动识别路径(避免中文路径)
3.打包exe文件 CMD: ..\Alex_Dome_D\Handle>pyinstaller -F Excel_handle.py 

修复:
1.首列合并

'''
# ======================================================================================================================
'''
# ======================================================================================================================
#   测试专用
# ======================================================================================================================
'''
if __name__ == "__main__":
    print('测试开始')
    # 获取当前文件路径 调试使用
    local_var_cwd    = os.getcwd()
    print('当前文件夹的路径    : < %s >' % local_var_cwd )
    [dir_name, file_name] = os.path.split(local_var_cwd)
    print('分离files & path : < %s >     < %s >'%(dir_name,file_name))
    # 固定文件夹目录

    excel_src_path   = local_var_cwd
    generate_xml_dir = dir_name

    #Translate_Excel_To_Xml(local_var_cwd + '\Alex_liszt.xlsx', 'Alex_liszt_Register.xlsx', excel_src_path)
    Translate_Excel_To_Xml(local_var_cwd + '\Liszt_Register_Definition_v2.0.xlsx', 'Liszt_Register_Definition_v2.0.xlsx', excel_src_path)

    # print('测试完成')

 

//------------------3.15

posted @ 2021-03-16 14:46  楚格  阅读(350)  评论(0编辑  收藏  举报