Get font type in python odfpy?

814 Views Asked by At

Have somebody experencies with odfpy ? I parsed document with this python package and I got paragraphs with his text and stylenames, now I need to detect which type of text font is in this paragraphs text?

Do you have any ideas ?

1

There are 1 best solutions below

0
On

The styles are defined separately from the text. The nodes that contain text will be inside nodes that have a style as an attribute. An example might look like this:

<text:p text:style-name="P5">
    <text:span text:style-name="T1">Do donkeys eat macadamia nuts? And if they don't, why don't they?
    </text:span>
</text:p>

In this example, two styles (P5 or T1) might specify a font for the text. You will need to look at the document's style definition section.

This code will create a dictionary containing the document's styles.

def get_styles(doc):
   styles= {}
   for ast in doc.automaticstyles.childNodes:

    name= ast.getAttribute('name')
    style= {}
    styles[name]= style

    for k in ast.attributes.keys():
        style[k[1]]= ast.attributes[k]
    for n in ast.childNodes:
        for k in n.attributes.keys():
            style[n.qname[1] + "/" + k[1]]= n.attributes[k]
    return styles

You can then examine the relevant styles that correspond to the text you care about. Inside each style will be a style:text-properties element, and that element will have a style:font-name attribute that specifies a font.