Text extents with Python Cairo

I needed this and could not find it. So I share it here (and even better if Google'd index it!)

def textwidth(text, fontsize=14):
        import cairo
    except Exception, e:
        return len(str) * fontsize
    surface = cairo.SVGSurface('undefined.svg', 1280, 200)
    cr = cairo.Context(surface)
    cr.select_font_face('Arial', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
    xbearing, ybearing, width, height, xadvance, yadvance = cr.text_extents(text)
    return width

#cairo, #python - Posted in the Dev category