简体   繁体   中英

LXML namespace in sub-elements formatted like XML spreadsheet

I am having trouble creating namespaces with LXML in Python. I am trying to convert specific data using Python from Excel to XML spreadsheet format.

I have created two sub-elements below. The second one ( WorksheetB ) is nearly what I need generated although I need a : between ss:Name . My experimental line is the first WorksheetA .

My goal is to get an XML tag that looks like this:

<WorksheetB ss:Name="This is worksheet B">

This is my code:

namespaces = {
    None:'urn:schemas-microsoft-com:office:spreadsheet',
    'o':'urn:schemas-microsoft-com:office:office',
    'x':'urn:schemas-microsoft-com:office:excel',
    'ss':'urn:schemas-microsoft-com:office:spreadsheet', 
    'html':"http://www.w3.org/TR/REC-html40"
}

root = ET.Element('Workbook', nsmap = namespaces)
WorksheetA = ET.SubElement(root, '{%s}Name' % 'urn:schemas-microsoft-com:office:spreadsheet')
Table = ET.SubElement(WorksheetA, 'Table')
Row = ET.SubElement(Table, 'Row')
Cell = ET.SubElement(Row, 'Cell')
Data = ET.SubElement(Cell, 'Data')
Data.text = '1'
Cell = ET.SubElement(Row, 'Cell')
Data = ET.SubElement(Cell, 'Data')
Data.text = '2'
WorksheetB = ET.SubElement(root, 'WorksheetB', ssName="This is worksheet B")
Table = ET.SubElement(WorksheetB, 'Table')
Row = ET.SubElement(Table, 'Row')
Cell = ET.SubElement(Row, 'Cell')
Data = ET.SubElement(Cell, 'Data')
Data.text = '3'
Cell = ET.SubElement(Row, 'Cell')
Data = ET.SubElement(Cell, 'Data')
Data.text = '4'
tree = ET.ElementTree(root)
tree.write(TestXMLFile, pretty_print=True, xml_declaration=True)

Here is the output:

<?xml version='1.0' encoding='ASCII'?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
  <ss:Name>
    <Table>
      <Row>
        <Cell>
          <Data>1</Data>
        </Cell>
        <Cell>
          <Data>2</Data>
        </Cell>
      </Row>
    </Table>
  </ss:Name>
  <WorksheetB ssName="This is worksheet B">
    <Table>
      <Row>
        <Cell>
          <Data>3</Data>
        </Cell>
        <Cell>
          <Data>4</Data>
        </Cell>
      </Row>
    </Table>
  </WorksheetB>
</Workbook>

If you need to use a namespace for an attribute of an Element or SubElement you can't pass it using the **kwargs ( **_extra ) syntax, as that does only allow to specify attributes with a name that is a valid python identifier.

So in this case you need to use the attrib argument to pass the attribute, eg:

...
WorksheetB = ET.SubElement(root, 'WorksheetB',
    {"{%s}Name" % namespaces['ss']: "This is worksheet B"})
...

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