aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/docx/styles/style.py
blob: aa175ea80547e1a82811c876c39d28b195615e3c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Style object hierarchy."""

from __future__ import annotations

from typing import Type

from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.styles import CT_Style
from docx.shared import ElementProxy
from docx.styles import BabelFish
from docx.text.font import Font
from docx.text.parfmt import ParagraphFormat


def StyleFactory(style_elm: CT_Style) -> BaseStyle:
    """Return `Style` object of appropriate |BaseStyle| subclass for `style_elm`."""
    style_cls: Type[BaseStyle] = {
        WD_STYLE_TYPE.PARAGRAPH: ParagraphStyle,
        WD_STYLE_TYPE.CHARACTER: CharacterStyle,
        WD_STYLE_TYPE.TABLE: _TableStyle,
        WD_STYLE_TYPE.LIST: _NumberingStyle,
    }[style_elm.type]

    return style_cls(style_elm)


class BaseStyle(ElementProxy):
    """Base class for the various types of style object, paragraph, character, table,
    and numbering.

    These properties and methods are inherited by all style objects.
    """

    def __init__(self, style_elm: CT_Style):
        super().__init__(style_elm)
        self._style_elm = style_elm

    @property
    def builtin(self):
        """Read-only.

        |True| if this style is a built-in style. |False| indicates it is a custom
        (user-defined) style. Note this value is based on the presence of a
        `customStyle` attribute in the XML, not on specific knowledge of which styles
        are built into Word.
        """
        return not self._element.customStyle

    def delete(self):
        """Remove this style definition from the document.

        Note that calling this method does not remove or change the style applied to any
        document content. Content items having the deleted style will be rendered using
        the default style, as is any content with a style not defined in the document.
        """
        self._element.delete()
        self._element = None

    @property
    def hidden(self):
        """|True| if display of this style in the style gallery and list of recommended
        styles is suppressed.

        |False| otherwise. In order to be shown in the style gallery, this value must be
        |False| and :attr:`.quick_style` must be |True|.
        """
        return self._element.semiHidden_val

    @hidden.setter
    def hidden(self, value):
        self._element.semiHidden_val = value

    @property
    def locked(self):
        """Read/write Boolean.

        |True| if this style is locked. A locked style does not appear in the styles
        panel or the style gallery and cannot be applied to document content. This
        behavior is only active when formatting protection is turned on for the document
        (via the Developer menu).
        """
        return self._element.locked_val

    @locked.setter
    def locked(self, value):
        self._element.locked_val = value

    @property
    def name(self):
        """The UI name of this style."""
        name = self._element.name_val
        if name is None:
            return None
        return BabelFish.internal2ui(name)

    @name.setter
    def name(self, value):
        self._element.name_val = value

    @property
    def priority(self):
        """The integer sort key governing display sequence of this style in the Word UI.

        |None| indicates no setting is defined, causing Word to use the default value of
        0. Style name is used as a secondary sort key to resolve ordering of styles
        having the same priority value.
        """
        return self._element.uiPriority_val

    @priority.setter
    def priority(self, value):
        self._element.uiPriority_val = value

    @property
    def quick_style(self):
        """|True| if this style should be displayed in the style gallery when
        :attr:`.hidden` is |False|.

        Read/write Boolean.
        """
        return self._element.qFormat_val

    @quick_style.setter
    def quick_style(self, value):
        self._element.qFormat_val = value

    @property
    def style_id(self) -> str:
        """The unique key name (string) for this style.

        This value is subject to rewriting by Word and should generally not be changed
        unless you are familiar with the internals involved.
        """
        return self._style_elm.styleId

    @style_id.setter
    def style_id(self, value):
        self._element.styleId = value

    @property
    def type(self):
        """Member of :ref:`WdStyleType` corresponding to the type of this style, e.g.
        ``WD_STYLE_TYPE.PARAGRAPH``."""
        type = self._style_elm.type
        if type is None:
            return WD_STYLE_TYPE.PARAGRAPH
        return type

    @property
    def unhide_when_used(self):
        """|True| if an application should make this style visible the next time it is
        applied to content.

        False otherwise. Note that |docx| does not automatically unhide a style having
        |True| for this attribute when it is applied to content.
        """
        return self._element.unhideWhenUsed_val

    @unhide_when_used.setter
    def unhide_when_used(self, value):
        self._element.unhideWhenUsed_val = value


class CharacterStyle(BaseStyle):
    """A character style.

    A character style is applied to a |Run| object and primarily provides character-
    level formatting via the |Font| object in its :attr:`.font` property.
    """

    @property
    def base_style(self):
        """Style object this style inherits from or |None| if this style is not based on
        another style."""
        base_style = self._element.base_style
        if base_style is None:
            return None
        return StyleFactory(base_style)

    @base_style.setter
    def base_style(self, style):
        style_id = style.style_id if style is not None else None
        self._element.basedOn_val = style_id

    @property
    def font(self):
        """The |Font| object providing access to the character formatting properties for
        this style, such as font name and size."""
        return Font(self._element)


# -- just in case someone uses the old name in an extension function --
_CharacterStyle = CharacterStyle


class ParagraphStyle(CharacterStyle):
    """A paragraph style.

    A paragraph style provides both character formatting and paragraph formatting such
    as indentation and line-spacing.
    """

    def __repr__(self):
        return "_ParagraphStyle('%s') id: %s" % (self.name, id(self))

    @property
    def next_paragraph_style(self):
        """|_ParagraphStyle| object representing the style to be applied automatically
        to a new paragraph inserted after a paragraph of this style.

        Returns self if no next paragraph style is defined. Assigning |None| or `self`
        removes the setting such that new paragraphs are created using this same style.
        """
        next_style_elm = self._element.next_style
        if next_style_elm is None:
            return self
        if next_style_elm.type != WD_STYLE_TYPE.PARAGRAPH:
            return self
        return StyleFactory(next_style_elm)

    @next_paragraph_style.setter
    def next_paragraph_style(self, style):
        if style is None or style.style_id == self.style_id:
            self._element._remove_next()
        else:
            self._element.get_or_add_next().val = style.style_id

    @property
    def paragraph_format(self):
        """The |ParagraphFormat| object providing access to the paragraph formatting
        properties for this style such as indentation."""
        return ParagraphFormat(self._element)


# -- just in case someone uses the old name in an extension function --
_ParagraphStyle = ParagraphStyle


class _TableStyle(ParagraphStyle):
    """A table style.

    A table style provides character and paragraph formatting for its contents as well
    as special table formatting properties.
    """

    def __repr__(self):
        return "_TableStyle('%s') id: %s" % (self.name, id(self))


class _NumberingStyle(BaseStyle):
    """A numbering style.

    Not yet implemented.
    """