我在使用Python中的LXML创建名称空间时遇到了麻烦。我正在尝试使用Python从Excel转换为XML电子表格格式的特定数据。
我在下面创建了两个子元素。第二个(WorksheetB
)几乎就是我需要生成的内容,尽管:
之间需要ss:Name
。我的实验行是第一个WorksheetA
。
我的目标是获得一个如下所示的XML标记:
<WorksheetB ss:Name="This is worksheet B">
这是我的代码:
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)
这是输出:
<?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>
答案 0 :(得分:0)
如果您需要为Element
或SubElement
的属性使用命名空间,则无法使用**kwargs
(**_extra
)语法将其传递给只允许指定名称是有效python标识符的属性。
因此,在这种情况下,您需要使用attrib
参数来传递属性,例如:
...
WorksheetB = ET.SubElement(root, 'WorksheetB',
{"{%s}Name" % namespaces['ss']: "This is worksheet B"})
...