================================================== ==========You can exploit the structure: the Elements you are looking for do not have
<data>
<fonts>
<fontData embed="true" name="Times" />
<fontData embed="true" name="Arial" />
</fonts>
<color>
</color>
<template>
<fonts>
<fontData>
<fontData embed="true" name="Courier">text</fontData>
</fontData>
<fontData embed="true" name="Helvetica" />
</fonts>
<width>
</width>
</template>
</data>
================================================== ==========
children, but they do have a "name" attribute.
declarations = [ fontData for fontData in root.getiterator('fontData')
if fontData.get("name") ]
This gives you a list of all "fontData" Elements that declare a font name. The
one you want is the last one, i.e. declarations[-1].
You can also do
font_names = [ fontData.get("name")
for fontData in root.getiterator('fontData')
if fontData.get("name") ]
or something like that. And if you only want the fontData Elements that appear
in the template Elements, try findall instead of getiterator:
tree.findall("//template//fontData")
("//" means: descend into the subtree, while "/" would mean: look only at the
direct children).
f = root.getiterator('fonts')You should not rely on ET returning a list from ".getiterator()", so avoid
n = f[-1].getiterator('fontData')
using f[-1] here.
fntList = []The first value (None) comes from the "fontData" Element that does not have a
f = root.getiterator('fonts')
n = f[-1].find('fontData')
for i in n:
fntList.append(i.get('name'))
print fntList
================================================== ==========
This code just gave me ['Courier']. Now if I change 'find' to 'findall' then I'll get [None, 'Helvetica']. Not exactly sure what exactly that's doing.
"name" attibute. The path expression "fontData" means: find all children that
are named "fontData". As above, what you want is ".//fontData".
Stefan