Friday, 30 January 2015

Multiple Elements in ElementMaker in lxml python



I wish to use ElementMaker in lxml to build an xml representation of an excel spreadsheet with the corresponding nesting. I would like



<excelbook>
workbook info
<excelsheet>
<sheetname>Sheet1</sheetname>
<exceltable>
<numrows>10</numrows>
<numcols>13</numcols>
</exceltable>
<exceltable>
<numrows>10</numrows>
<numcols>13</numcols>
</exceltable>
</excelsheet>
<excelsheet>
<sheetname>Sheet2</sheetname>
<exceltable>
<numrows>10</numrows>
<numcols>13</numcols>
</exceltable>
<exceltable>
<numrows>10</numrows>
<numcols>13</numcols>
</exceltable>
</excelsheet>
</excelbook>


My python code looks like the following



for excelSheet in excelBook.excelSheets:

for excelTable in excelSheet.excelTables:

exceltable = E.exceltable(
E.num_rows(str(excelTable.num_rows)),
E.num_cols(str(excelTable.num_cols)),
)

excelsheet = E.excelsheet(
exceltable,
E.sheetname(excelSheet.sheetName),
)

excelbook = E.excelbook(
excelsheet,
E.bookname(fullpathname),
E.numSheets(str(excelBook.numSheets)))

root = E.XML(excelbook)


The problem is that I can only nest one sheet inside each book and one table inside each sheet. How do I change the code to allow multiple sheets in each book and multiple tables inside each sheet.


No comments:

Post a Comment