简体   繁体   中英

How to import a table in XML format to excel file?

I have a bunch of tables in an XML file that need to be imported to excel file. I tried importing the tables directly using the power query but it did not work. There are a total of 25 such tables that I need to import to my excel sheet. I have minimal knowledge of VBA or python scripting and hence I am clueless of how to proceed. Any help in this regard would be really helpful

Here is an example of the tables in XML format.

    <TABLE VIEW="DETAILED"><TABLE-CAPTION ID="ID19862004604572"><LONG-NAME> 
                                <L-4 L="en">BORE_TUNE1</L-4></LONG-NAME></TABLE-CAPTION><TGROUP COLS="1"><COLSPEC COLNUM="1" COLNAME="col1" COLWIDTH="1.00*"/><COLSPEC COLNUM="2" COLNAME="col2" COLWIDTH="1.00*"/><TBODY>
<ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">X</E></L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">0.3</E></L-1> 
                                     </P></ENTRY></ROW><ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all">Value</L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all">0.5</L-1> 
                                     </P></ENTRY></ROW></TBODY></TGROUP> 
                        </TABLE>
                        <TABLE VIEW="DETAILED"><TABLE-CAPTION ID="ID19862004604573"><LONG-NAME> 
                                <L-4 L="en">BORE_TUNE2</L-4></LONG-NAME></TABLE-CAPTION><TGROUP COLS="2"><COLSPEC COLNUM="1" COLNAME="col1" COLWIDTH="1.00*"/><COLSPEC COLNUM="2" COLNAME="col2" COLWIDTH="1.00*"/><COLSPEC COLNUM="3" COLNAME="col3" COLWIDTH="1.00*"/><TBODY>
<ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">X</E></L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">0.3</E></L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col3">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">0.8</E></L-1> 
                                     </P></ENTRY></ROW><ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all">Value</L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all">0.5</L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col3">
                                     <P> 
                                        <L-1 L="for-all">0.5</L-1> 
                                     </P></ENTRY></ROW></TBODY></TGROUP> 
                        </TABLE>
                        <TABLE VIEW="DETAILED"><TABLE-CAPTION ID="ID19862004604574"><LONG-NAME> 
                                <L-4 L="en">BORE_TUNE3</L-4></LONG-NAME></TABLE-CAPTION><TGROUP COLS="1"><COLSPEC COLNUM="1" COLNAME="col1" COLWIDTH="1.00*"/><COLSPEC COLNUM="2" COLNAME="col2" COLWIDTH="1.00*"/><TBODY>
<ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">X</E></L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">0.3</E></L-1> 
                                     </P></ENTRY></ROW><ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all">Value</L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all">0.5</L-1> 
                                     </P></ENTRY></ROW></TBODY></TGROUP> 
                        </TABLE>
                        <TABLE VIEW="DETAILED"><TABLE-CAPTION ID="ID19862004604575"><LONG-NAME> 
                                <L-4 L="en">BORE_TUNE4</L-4></LONG-NAME></TABLE-CAPTION><TGROUP COLS="1"><COLSPEC COLNUM="1" COLNAME="col1" COLWIDTH="1.00*"/><COLSPEC COLNUM="2" COLNAME="col2" COLWIDTH="1.00*"/><TBODY>
<ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">X</E></L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all"><E TYPE="BOLD">0.3</E></L-1> 
                                     </P></ENTRY></ROW><ROW><ENTRY COLNAME="col1">
                                     <P> 
                                        <L-1 L="for-all">Value</L-1> 
                                     </P></ENTRY><ENTRY COLNAME="col2">
                                     <P> 
                                        <L-1 L="for-all">0.5</L-1> 
                                     </P></ENTRY></ROW></TBODY></TGROUP> 
                        </TABLE>

It needs to look as the image attached (in excel file) 在此处输入图像描述

You can use xmltodict lib to parse your XML and then save it to Excel file:

import xmltodict
import xlsxwriter

workbook = xlsxwriter.Workbook('Output.xlsx')
worksheet = workbook.add_worksheet()
res = []
with open('file.xml', 'r') as f:
    data = xmltodict.parse(f.read())
    worksheet.write(0, 0, data['TABLE']['TABLE-CAPTION']['LONG-NAME']['L-4']['#text'])
    for row in data['TABLE']['TGROUP']['TBODY']['ROW']:
        for entry in row['ENTRY']:
            if entry['P']['L-1'].get('E'):
                res.append(entry['P']['L-1']['E']['#text'])
            else:
                res.append(entry['P']['L-1']['#text'])

for r in range(int(len(res)/2)):
    for c in range(2):
        worksheet.write(r+1, c, res[r+c])

workbook.close()

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM