Skip to content

Content

Representation of the “content.xml” part.

Classes:

Name Description
Content

Representation of the “content.xml” part.

Content

Bases: XmlPart

Representation of the “content.xml” part.

Methods:

Name Description
__str__
get_style

Return the style uniquely identified by the name/family pair. If the

get_styles

Return the list of styles in the Content part, optionally limited to

Source code in odfdo/content.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class Content(XmlPart):
    """Representation of the "content.xml" part."""

    def _get_style_contexts(self, family: str | None) -> tuple:
        if family == "font-face":
            return (self.get_element("//office:font-face-decls"),)
        return (
            self.get_element("//office:font-face-decls"),
            self.get_element("//office:automatic-styles"),
        )

    def __str__(self) -> str:
        return str(self.body)

    # Public API

    def get_styles(self, family: str | None = None) -> list[StyleBase]:
        """Return the list of styles in the Content part, optionally limited to
        the given family.

        Args:
            family: The style family to filter by.

        Returns:
            A list of styles.
        """
        result: list[StyleBase] = []
        for context in self._get_style_contexts(family):
            if context is None:
                continue
            result.extend(context.get_styles(family=family))
        return result

    def get_style(
        self,
        family: str,
        name_or_element: str | Element | None = None,
        display_name: str | None = None,
    ) -> StyleBase | None:
        """Return the style uniquely identified by the name/family pair. If the
        argument is already a style object, it will return it.

        If the name is None, the default style is fetched.

        If the name is not the internal name but the name you gave in the
        desktop application, use display_name instead.

        Args:
            family: The family of the style to retrieve (e.g., 'paragraph',
                'text', 'graphic').
            name_or_element: The name of the style or a Style element. If
                None, the default style for the family is returned.
            display_name: The display name of the style to search for.

        Returns:
            The Style object, or None if not found.
        """
        for context in self._get_style_contexts(family):
            if context is None:
                continue
            style: StyleBase | None = context.get_style(
                family,
                name_or_element=name_or_element,
                display_name=display_name,
            )
            if style is not None:
                return style
        return None

__str__

__str__() -> str
Source code in odfdo/content.py
48
49
def __str__(self) -> str:
    return str(self.body)

_get_style_contexts

_get_style_contexts(family: str | None) -> tuple
Source code in odfdo/content.py
40
41
42
43
44
45
46
def _get_style_contexts(self, family: str | None) -> tuple:
    if family == "font-face":
        return (self.get_element("//office:font-face-decls"),)
    return (
        self.get_element("//office:font-face-decls"),
        self.get_element("//office:automatic-styles"),
    )

get_style

get_style(
    family: str,
    name_or_element: str | Element | None = None,
    display_name: str | None = None,
) -> StyleBase | None

Return the style uniquely identified by the name/family pair. If the argument is already a style object, it will return it.

If the name is None, the default style is fetched.

If the name is not the internal name but the name you gave in the desktop application, use display_name instead.

Parameters:

Name Type Description Default
family str

The family of the style to retrieve (e.g., ‘paragraph’, ‘text’, ‘graphic’).

required
name_or_element str | Element | None

The name of the style or a Style element. If None, the default style for the family is returned.

None
display_name str | None

The display name of the style to search for.

None

Returns:

Type Description
StyleBase | None

The Style object, or None if not found.

Source code in odfdo/content.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def get_style(
    self,
    family: str,
    name_or_element: str | Element | None = None,
    display_name: str | None = None,
) -> StyleBase | None:
    """Return the style uniquely identified by the name/family pair. If the
    argument is already a style object, it will return it.

    If the name is None, the default style is fetched.

    If the name is not the internal name but the name you gave in the
    desktop application, use display_name instead.

    Args:
        family: The family of the style to retrieve (e.g., 'paragraph',
            'text', 'graphic').
        name_or_element: The name of the style or a Style element. If
            None, the default style for the family is returned.
        display_name: The display name of the style to search for.

    Returns:
        The Style object, or None if not found.
    """
    for context in self._get_style_contexts(family):
        if context is None:
            continue
        style: StyleBase | None = context.get_style(
            family,
            name_or_element=name_or_element,
            display_name=display_name,
        )
        if style is not None:
            return style
    return None

get_styles

get_styles(family: str | None = None) -> list[StyleBase]

Return the list of styles in the Content part, optionally limited to the given family.

Parameters:

Name Type Description Default
family str | None

The style family to filter by.

None

Returns:

Type Description
list[StyleBase]

A list of styles.

Source code in odfdo/content.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def get_styles(self, family: str | None = None) -> list[StyleBase]:
    """Return the list of styles in the Content part, optionally limited to
    the given family.

    Args:
        family: The style family to filter by.

    Returns:
        A list of styles.
    """
    result: list[StyleBase] = []
    for context in self._get_style_contexts(family):
        if context is None:
            continue
        result.extend(context.get_styles(family=family))
    return result