ps-python-docx 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. docx/__init__.py +68 -0
  2. docx/api.py +37 -0
  3. docx/blkcntnr.py +101 -0
  4. docx/comments.py +163 -0
  5. docx/dml/__init__.py +0 -0
  6. docx/dml/color.py +112 -0
  7. docx/document.py +275 -0
  8. docx/drawing/__init__.py +59 -0
  9. docx/enum/__init__.py +0 -0
  10. docx/enum/base.py +150 -0
  11. docx/enum/dml.py +103 -0
  12. docx/enum/section.py +86 -0
  13. docx/enum/shape.py +19 -0
  14. docx/enum/style.py +452 -0
  15. docx/enum/table.py +136 -0
  16. docx/enum/text.py +367 -0
  17. docx/exceptions.py +18 -0
  18. docx/image/__init__.py +23 -0
  19. docx/image/bmp.py +43 -0
  20. docx/image/constants.py +172 -0
  21. docx/image/exceptions.py +13 -0
  22. docx/image/gif.py +38 -0
  23. docx/image/helpers.py +86 -0
  24. docx/image/image.py +234 -0
  25. docx/image/jpeg.py +425 -0
  26. docx/image/png.py +253 -0
  27. docx/image/tiff.py +289 -0
  28. docx/opc/__init__.py +0 -0
  29. docx/opc/constants.py +306 -0
  30. docx/opc/coreprops.py +142 -0
  31. docx/opc/exceptions.py +12 -0
  32. docx/opc/oxml.py +247 -0
  33. docx/opc/package.py +219 -0
  34. docx/opc/packuri.py +109 -0
  35. docx/opc/part.py +247 -0
  36. docx/opc/parts/__init__.py +0 -0
  37. docx/opc/parts/coreprops.py +48 -0
  38. docx/opc/phys_pkg.py +119 -0
  39. docx/opc/pkgreader.py +254 -0
  40. docx/opc/pkgwriter.py +115 -0
  41. docx/opc/rel.py +153 -0
  42. docx/opc/shared.py +31 -0
  43. docx/opc/spec.py +24 -0
  44. docx/oxml/__init__.py +261 -0
  45. docx/oxml/comments.py +124 -0
  46. docx/oxml/coreprops.py +298 -0
  47. docx/oxml/document.py +88 -0
  48. docx/oxml/drawing.py +11 -0
  49. docx/oxml/exceptions.py +10 -0
  50. docx/oxml/ns.py +109 -0
  51. docx/oxml/numbering.py +109 -0
  52. docx/oxml/parser.py +62 -0
  53. docx/oxml/section.py +537 -0
  54. docx/oxml/settings.py +138 -0
  55. docx/oxml/shape.py +299 -0
  56. docx/oxml/shared.py +52 -0
  57. docx/oxml/simpletypes.py +434 -0
  58. docx/oxml/styles.py +341 -0
  59. docx/oxml/table.py +977 -0
  60. docx/oxml/text/__init__.py +0 -0
  61. docx/oxml/text/font.py +333 -0
  62. docx/oxml/text/hyperlink.py +45 -0
  63. docx/oxml/text/pagebreak.py +278 -0
  64. docx/oxml/text/paragraph.py +106 -0
  65. docx/oxml/text/parfmt.py +392 -0
  66. docx/oxml/text/run.py +307 -0
  67. docx/oxml/xmlchemy.py +696 -0
  68. docx/package.py +110 -0
  69. docx/parts/__init__.py +0 -0
  70. docx/parts/comments.py +51 -0
  71. docx/parts/document.py +182 -0
  72. docx/parts/hdrftr.py +53 -0
  73. docx/parts/image.py +80 -0
  74. docx/parts/numbering.py +32 -0
  75. docx/parts/settings.py +50 -0
  76. docx/parts/story.py +95 -0
  77. docx/parts/styles.py +42 -0
  78. docx/parts/theme.py +53 -0
  79. docx/py.typed +0 -0
  80. docx/section.py +479 -0
  81. docx/settings.py +35 -0
  82. docx/shape.py +103 -0
  83. docx/shared.py +382 -0
  84. docx/styles/__init__.py +40 -0
  85. docx/styles/latent.py +198 -0
  86. docx/styles/style.py +264 -0
  87. docx/styles/styles.py +147 -0
  88. docx/table.py +537 -0
  89. docx/templates/default-comments.xml +12 -0
  90. docx/templates/default-docx-template/[Content_Types].xml +17 -0
  91. docx/templates/default-docx-template/_rels/.rels +7 -0
  92. docx/templates/default-docx-template/customXml/_rels/item1.xml.rels +4 -0
  93. docx/templates/default-docx-template/customXml/item1.xml +2 -0
  94. docx/templates/default-docx-template/customXml/itemProps1.xml +6 -0
  95. docx/templates/default-docx-template/docProps/app.xml +36 -0
  96. docx/templates/default-docx-template/docProps/core.xml +13 -0
  97. docx/templates/default-docx-template/docProps/thumbnail.jpeg +0 -0
  98. docx/templates/default-docx-template/word/_rels/document.xml.rels +11 -0
  99. docx/templates/default-docx-template/word/document.xml +11 -0
  100. docx/templates/default-docx-template/word/fontTable.xml +61 -0
  101. docx/templates/default-docx-template/word/numbering.xml +201 -0
  102. docx/templates/default-docx-template/word/settings.xml +53 -0
  103. docx/templates/default-docx-template/word/styles.xml +11844 -0
  104. docx/templates/default-docx-template/word/stylesWithEffects.xml +11800 -0
  105. docx/templates/default-docx-template/word/theme/theme1.xml +318 -0
  106. docx/templates/default-docx-template/word/webSettings.xml +5 -0
  107. docx/templates/default-footer.xml +27 -0
  108. docx/templates/default-header.xml +27 -0
  109. docx/templates/default-settings.xml +26 -0
  110. docx/templates/default-styles.xml +190 -0
  111. docx/templates/default.docx +0 -0
  112. docx/text/__init__.py +0 -0
  113. docx/text/font.py +472 -0
  114. docx/text/hyperlink.py +121 -0
  115. docx/text/pagebreak.py +104 -0
  116. docx/text/paragraph.py +173 -0
  117. docx/text/parfmt.py +286 -0
  118. docx/text/run.py +257 -0
  119. docx/text/tabstops.py +123 -0
  120. docx/theme.py +67 -0
  121. docx/types.py +34 -0
  122. ps_python_docx-1.3.0.dist-info/METADATA +77 -0
  123. ps_python_docx-1.3.0.dist-info/RECORD +126 -0
  124. ps_python_docx-1.3.0.dist-info/WHEEL +5 -0
  125. ps_python_docx-1.3.0.dist-info/licenses/LICENSE +20 -0
  126. ps_python_docx-1.3.0.dist-info/top_level.txt +1 -0
docx/styles/latent.py ADDED
@@ -0,0 +1,198 @@
1
+ """Latent style-related objects."""
2
+
3
+ from docx.shared import ElementProxy
4
+ from docx.styles import BabelFish
5
+
6
+
7
+ class LatentStyles(ElementProxy):
8
+ """Provides access to the default behaviors for latent styles in this document and
9
+ to the collection of |_LatentStyle| objects that define overrides of those defaults
10
+ for a particular named latent style."""
11
+
12
+ def __getitem__(self, key):
13
+ """Enables dictionary-style access to a latent style by name."""
14
+ style_name = BabelFish.ui2internal(key)
15
+ lsdException = self._element.get_by_name(style_name)
16
+ if lsdException is None:
17
+ raise KeyError("no latent style with name '%s'" % key)
18
+ return _LatentStyle(lsdException)
19
+
20
+ def __iter__(self):
21
+ return (_LatentStyle(ls) for ls in self._element.lsdException_lst)
22
+
23
+ def __len__(self):
24
+ return len(self._element.lsdException_lst)
25
+
26
+ def add_latent_style(self, name):
27
+ """Return a newly added |_LatentStyle| object to override the inherited defaults
28
+ defined in this latent styles object for the built-in style having `name`."""
29
+ lsdException = self._element.add_lsdException()
30
+ lsdException.name = BabelFish.ui2internal(name)
31
+ return _LatentStyle(lsdException)
32
+
33
+ @property
34
+ def default_priority(self):
35
+ """Integer between 0 and 99 inclusive specifying the default sort order for
36
+ latent styles in style lists and the style gallery.
37
+
38
+ |None| if no value is assigned, which causes Word to use the default value 99.
39
+ """
40
+ return self._element.defUIPriority
41
+
42
+ @default_priority.setter
43
+ def default_priority(self, value):
44
+ self._element.defUIPriority = value
45
+
46
+ @property
47
+ def default_to_hidden(self):
48
+ """Boolean specifying whether the default behavior for latent styles is to be
49
+ hidden.
50
+
51
+ A hidden style does not appear in the recommended list or in the style gallery.
52
+ """
53
+ return self._element.bool_prop("defSemiHidden")
54
+
55
+ @default_to_hidden.setter
56
+ def default_to_hidden(self, value):
57
+ self._element.set_bool_prop("defSemiHidden", value)
58
+
59
+ @property
60
+ def default_to_locked(self):
61
+ """Boolean specifying whether the default behavior for latent styles is to be
62
+ locked.
63
+
64
+ A locked style does not appear in the styles panel or the style gallery and
65
+ cannot be applied to document content. This behavior is only active when
66
+ formatting protection is turned on for the document (via the Developer menu).
67
+ """
68
+ return self._element.bool_prop("defLockedState")
69
+
70
+ @default_to_locked.setter
71
+ def default_to_locked(self, value):
72
+ self._element.set_bool_prop("defLockedState", value)
73
+
74
+ @property
75
+ def default_to_quick_style(self):
76
+ """Boolean specifying whether the default behavior for latent styles is to
77
+ appear in the style gallery when not hidden."""
78
+ return self._element.bool_prop("defQFormat")
79
+
80
+ @default_to_quick_style.setter
81
+ def default_to_quick_style(self, value):
82
+ self._element.set_bool_prop("defQFormat", value)
83
+
84
+ @property
85
+ def default_to_unhide_when_used(self):
86
+ """Boolean specifying whether the default behavior for latent styles is to be
87
+ unhidden when first applied to content."""
88
+ return self._element.bool_prop("defUnhideWhenUsed")
89
+
90
+ @default_to_unhide_when_used.setter
91
+ def default_to_unhide_when_used(self, value):
92
+ self._element.set_bool_prop("defUnhideWhenUsed", value)
93
+
94
+ @property
95
+ def load_count(self):
96
+ """Integer specifying the number of built-in styles to initialize to the
97
+ defaults specified in this |LatentStyles| object.
98
+
99
+ |None| if there is no setting in the XML (very uncommon). The default Word 2011
100
+ template sets this value to 276, accounting for the built-in styles in Word
101
+ 2010.
102
+ """
103
+ return self._element.count
104
+
105
+ @load_count.setter
106
+ def load_count(self, value):
107
+ self._element.count = value
108
+
109
+
110
+ class _LatentStyle(ElementProxy):
111
+ """Proxy for an `w:lsdException` element, which specifies display behaviors for a
112
+ built-in style when no definition for that style is stored yet in the `styles.xml`
113
+ part.
114
+
115
+ The values in this element override the defaults specified in the parent
116
+ `w:latentStyles` element.
117
+ """
118
+
119
+ def delete(self):
120
+ """Remove this latent style definition such that the defaults defined in the
121
+ containing |LatentStyles| object provide the effective value for each of its
122
+ attributes.
123
+
124
+ Attempting to access any attributes on this object after calling this method
125
+ will raise |AttributeError|.
126
+ """
127
+ self._element.delete()
128
+ self._element = None
129
+
130
+ @property
131
+ def hidden(self):
132
+ """Tri-state value specifying whether this latent style should appear in the
133
+ recommended list.
134
+
135
+ |None| indicates the effective value is inherited from the parent
136
+ ``<w:latentStyles>`` element.
137
+ """
138
+ return self._element.on_off_prop("semiHidden")
139
+
140
+ @hidden.setter
141
+ def hidden(self, value):
142
+ self._element.set_on_off_prop("semiHidden", value)
143
+
144
+ @property
145
+ def locked(self):
146
+ """Tri-state value specifying whether this latent styles is locked.
147
+
148
+ A locked style does not appear in the styles panel or the style gallery and
149
+ cannot be applied to document content. This behavior is only active when
150
+ formatting protection is turned on for the document (via the Developer menu).
151
+ """
152
+ return self._element.on_off_prop("locked")
153
+
154
+ @locked.setter
155
+ def locked(self, value):
156
+ self._element.set_on_off_prop("locked", value)
157
+
158
+ @property
159
+ def name(self):
160
+ """The name of the built-in style this exception applies to."""
161
+ return BabelFish.internal2ui(self._element.name)
162
+
163
+ @property
164
+ def priority(self):
165
+ """The integer sort key for this latent style in the Word UI."""
166
+ return self._element.uiPriority
167
+
168
+ @priority.setter
169
+ def priority(self, value):
170
+ self._element.uiPriority = value
171
+
172
+ @property
173
+ def quick_style(self):
174
+ """Tri-state value specifying whether this latent style should appear in the
175
+ Word styles gallery when not hidden.
176
+
177
+ |None| indicates the effective value should be inherited from the default values
178
+ in its parent |LatentStyles| object.
179
+ """
180
+ return self._element.on_off_prop("qFormat")
181
+
182
+ @quick_style.setter
183
+ def quick_style(self, value):
184
+ self._element.set_on_off_prop("qFormat", value)
185
+
186
+ @property
187
+ def unhide_when_used(self):
188
+ """Tri-state value specifying whether this style should have its :attr:`hidden`
189
+ attribute set |False| the next time the style is applied to content.
190
+
191
+ |None| indicates the effective value should be inherited from the default
192
+ specified by its parent |LatentStyles| object.
193
+ """
194
+ return self._element.on_off_prop("unhideWhenUsed")
195
+
196
+ @unhide_when_used.setter
197
+ def unhide_when_used(self, value):
198
+ self._element.set_on_off_prop("unhideWhenUsed", value)
docx/styles/style.py ADDED
@@ -0,0 +1,264 @@
1
+ """Style object hierarchy."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Type
6
+
7
+ from docx.enum.style import WD_STYLE_TYPE
8
+ from docx.oxml.styles import CT_Style
9
+ from docx.shared import ElementProxy
10
+ from docx.styles import BabelFish
11
+ from docx.text.font import Font
12
+ from docx.text.parfmt import ParagraphFormat
13
+
14
+
15
+ def StyleFactory(style_elm: CT_Style) -> BaseStyle:
16
+ """Return `Style` object of appropriate |BaseStyle| subclass for `style_elm`."""
17
+ style_cls: Type[BaseStyle] = {
18
+ WD_STYLE_TYPE.PARAGRAPH: ParagraphStyle,
19
+ WD_STYLE_TYPE.CHARACTER: CharacterStyle,
20
+ WD_STYLE_TYPE.TABLE: _TableStyle,
21
+ WD_STYLE_TYPE.LIST: _NumberingStyle,
22
+ }[style_elm.type]
23
+
24
+ return style_cls(style_elm)
25
+
26
+
27
+ class BaseStyle(ElementProxy):
28
+ """Base class for the various types of style object, paragraph, character, table,
29
+ and numbering.
30
+
31
+ These properties and methods are inherited by all style objects.
32
+ """
33
+
34
+ def __init__(self, style_elm: CT_Style):
35
+ super().__init__(style_elm)
36
+ self._style_elm = style_elm
37
+
38
+ @property
39
+ def builtin(self):
40
+ """Read-only.
41
+
42
+ |True| if this style is a built-in style. |False| indicates it is a custom
43
+ (user-defined) style. Note this value is based on the presence of a
44
+ `customStyle` attribute in the XML, not on specific knowledge of which styles
45
+ are built into Word.
46
+ """
47
+ return not self._element.customStyle
48
+
49
+ def delete(self):
50
+ """Remove this style definition from the document.
51
+
52
+ Note that calling this method does not remove or change the style applied to any
53
+ document content. Content items having the deleted style will be rendered using
54
+ the default style, as is any content with a style not defined in the document.
55
+ """
56
+ self._element.delete()
57
+ self._element = None
58
+
59
+ @property
60
+ def hidden(self):
61
+ """|True| if display of this style in the style gallery and list of recommended
62
+ styles is suppressed.
63
+
64
+ |False| otherwise. In order to be shown in the style gallery, this value must be
65
+ |False| and :attr:`.quick_style` must be |True|.
66
+ """
67
+ return self._element.semiHidden_val
68
+
69
+ @hidden.setter
70
+ def hidden(self, value):
71
+ self._element.semiHidden_val = value
72
+
73
+ @property
74
+ def linked_style(self) -> BaseStyle | None:
75
+ """Read-only linked style, or |None| if no valid target is defined.
76
+
77
+ Paragraph styles can have a linked character style used when applying the
78
+ style to selected text. Changing one does not automatically change the other.
79
+ """
80
+ linked = self._element.linked_style
81
+ return None if linked is None else StyleFactory(linked)
82
+
83
+ @property
84
+ def locked(self):
85
+ """Read/write Boolean.
86
+
87
+ |True| if this style is locked. A locked style does not appear in the styles
88
+ panel or the style gallery and cannot be applied to document content. This
89
+ behavior is only active when formatting protection is turned on for the document
90
+ (via the Developer menu).
91
+ """
92
+ return self._element.locked_val
93
+
94
+ @locked.setter
95
+ def locked(self, value):
96
+ self._element.locked_val = value
97
+
98
+ @property
99
+ def name(self):
100
+ """The UI name of this style."""
101
+ name = self._element.name_val
102
+ if name is None:
103
+ return None
104
+ return BabelFish.internal2ui(name)
105
+
106
+ @name.setter
107
+ def name(self, value):
108
+ self._element.name_val = value
109
+
110
+ @property
111
+ def priority(self):
112
+ """The integer sort key governing display sequence of this style in the Word UI.
113
+
114
+ |None| indicates no setting is defined, causing Word to use the default value of
115
+ 0. Style name is used as a secondary sort key to resolve ordering of styles
116
+ having the same priority value.
117
+ """
118
+ return self._element.uiPriority_val
119
+
120
+ @priority.setter
121
+ def priority(self, value):
122
+ self._element.uiPriority_val = value
123
+
124
+ @property
125
+ def quick_style(self):
126
+ """|True| if this style should be displayed in the style gallery when
127
+ :attr:`.hidden` is |False|.
128
+
129
+ Read/write Boolean.
130
+ """
131
+ return self._element.qFormat_val
132
+
133
+ @quick_style.setter
134
+ def quick_style(self, value):
135
+ self._element.qFormat_val = value
136
+
137
+ @property
138
+ def style_id(self) -> str:
139
+ """The unique key name (string) for this style.
140
+
141
+ This value is subject to rewriting by Word and should generally not be changed
142
+ unless you are familiar with the internals involved.
143
+ """
144
+ return self._style_elm.styleId
145
+
146
+ @style_id.setter
147
+ def style_id(self, value):
148
+ self._element.styleId = value
149
+
150
+ @property
151
+ def type(self):
152
+ """Member of :ref:`WdStyleType` corresponding to the type of this style, e.g.
153
+ ``WD_STYLE_TYPE.PARAGRAPH``."""
154
+ type = self._style_elm.type
155
+ if type is None:
156
+ return WD_STYLE_TYPE.PARAGRAPH
157
+ return type
158
+
159
+ @property
160
+ def unhide_when_used(self):
161
+ """|True| if an application should make this style visible the next time it is
162
+ applied to content.
163
+
164
+ False otherwise. Note that |docx| does not automatically unhide a style having
165
+ |True| for this attribute when it is applied to content.
166
+ """
167
+ return self._element.unhideWhenUsed_val
168
+
169
+ @unhide_when_used.setter
170
+ def unhide_when_used(self, value):
171
+ self._element.unhideWhenUsed_val = value
172
+
173
+
174
+ class CharacterStyle(BaseStyle):
175
+ """A character style.
176
+
177
+ A character style is applied to a |Run| object and primarily provides character-
178
+ level formatting via the |Font| object in its :attr:`.font` property.
179
+ """
180
+
181
+ @property
182
+ def base_style(self):
183
+ """Style object this style inherits from or |None| if this style is not based on
184
+ another style."""
185
+ base_style = self._element.base_style
186
+ if base_style is None:
187
+ return None
188
+ return StyleFactory(base_style)
189
+
190
+ @base_style.setter
191
+ def base_style(self, style):
192
+ style_id = style.style_id if style is not None else None
193
+ self._element.basedOn_val = style_id
194
+
195
+ @property
196
+ def font(self):
197
+ """The |Font| object providing access to the character formatting properties for
198
+ this style, such as font name and size."""
199
+ return Font(self._element)
200
+
201
+
202
+ # -- just in case someone uses the old name in an extension function --
203
+ _CharacterStyle = CharacterStyle
204
+
205
+
206
+ class ParagraphStyle(CharacterStyle):
207
+ """A paragraph style.
208
+
209
+ A paragraph style provides both character formatting and paragraph formatting such
210
+ as indentation and line-spacing.
211
+ """
212
+
213
+ def __repr__(self):
214
+ return "_ParagraphStyle('%s') id: %s" % (self.name, id(self))
215
+
216
+ @property
217
+ def next_paragraph_style(self):
218
+ """|_ParagraphStyle| object representing the style to be applied automatically
219
+ to a new paragraph inserted after a paragraph of this style.
220
+
221
+ Returns self if no next paragraph style is defined. Assigning |None| or `self`
222
+ removes the setting such that new paragraphs are created using this same style.
223
+ """
224
+ next_style_elm = self._element.next_style
225
+ if next_style_elm is None:
226
+ return self
227
+ if next_style_elm.type != WD_STYLE_TYPE.PARAGRAPH:
228
+ return self
229
+ return StyleFactory(next_style_elm)
230
+
231
+ @next_paragraph_style.setter
232
+ def next_paragraph_style(self, style):
233
+ if style is None or style.style_id == self.style_id:
234
+ self._element._remove_next()
235
+ else:
236
+ self._element.get_or_add_next().val = style.style_id
237
+
238
+ @property
239
+ def paragraph_format(self):
240
+ """The |ParagraphFormat| object providing access to the paragraph formatting
241
+ properties for this style such as indentation."""
242
+ return ParagraphFormat(self._element)
243
+
244
+
245
+ # -- just in case someone uses the old name in an extension function --
246
+ _ParagraphStyle = ParagraphStyle
247
+
248
+
249
+ class _TableStyle(ParagraphStyle):
250
+ """A table style.
251
+
252
+ A table style provides character and paragraph formatting for its contents as well
253
+ as special table formatting properties.
254
+ """
255
+
256
+ def __repr__(self):
257
+ return "_TableStyle('%s') id: %s" % (self.name, id(self))
258
+
259
+
260
+ class _NumberingStyle(BaseStyle):
261
+ """A numbering style.
262
+
263
+ Not yet implemented.
264
+ """
docx/styles/styles.py ADDED
@@ -0,0 +1,147 @@
1
+ """Styles object, container for all objects in the styles part."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from warnings import warn
6
+
7
+ from docx.enum.style import WD_STYLE_TYPE
8
+ from docx.oxml.styles import CT_Styles
9
+ from docx.shared import ElementProxy
10
+ from docx.styles import BabelFish
11
+ from docx.styles.latent import LatentStyles
12
+ from docx.styles.style import BaseStyle, StyleFactory
13
+ from docx.text.font import Font
14
+
15
+
16
+ class Styles(ElementProxy):
17
+ """Provides access to the styles defined in a document.
18
+
19
+ Accessed using the :attr:`.Document.styles` property. Supports ``len()``, iteration,
20
+ and dictionary-style access by style name.
21
+ """
22
+
23
+ def __init__(self, styles: CT_Styles):
24
+ super().__init__(styles)
25
+ self._element = styles
26
+
27
+ def __contains__(self, name):
28
+ """Enables `in` operator on style name."""
29
+ internal_name = BabelFish.ui2internal(name)
30
+ return any(style.name_val == internal_name for style in self._element.style_lst)
31
+
32
+ def __getitem__(self, key: str):
33
+ """Enables dictionary-style access by UI name.
34
+
35
+ Lookup by style id is deprecated, triggers a warning, and will be removed in a
36
+ near-future release.
37
+ """
38
+ style_elm = self._element.get_by_name(BabelFish.ui2internal(key))
39
+ if style_elm is not None:
40
+ return StyleFactory(style_elm)
41
+
42
+ style_elm = self._element.get_by_id(key)
43
+ if style_elm is not None:
44
+ msg = "style lookup by style_id is deprecated. Use style name as key instead."
45
+ warn(msg, UserWarning, stacklevel=2)
46
+ return StyleFactory(style_elm)
47
+
48
+ raise KeyError("no style with name '%s'" % key)
49
+
50
+ def __iter__(self):
51
+ return (StyleFactory(style) for style in self._element.style_lst)
52
+
53
+ def __len__(self):
54
+ return len(self._element.style_lst)
55
+
56
+ def add_style(self, name, style_type, builtin=False):
57
+ """Return a newly added style object of `style_type` and identified by `name`.
58
+
59
+ A builtin style can be defined by passing True for the optional `builtin`
60
+ argument.
61
+ """
62
+ style_name = BabelFish.ui2internal(name)
63
+ if style_name in self:
64
+ raise ValueError("document already contains style '%s'" % name)
65
+ style = self._element.add_style_of_type(style_name, style_type, builtin)
66
+ return StyleFactory(style)
67
+
68
+ def default(self, style_type: WD_STYLE_TYPE):
69
+ """Return the default style for `style_type` or |None| if no default is defined
70
+ for that type (not common)."""
71
+ style = self._element.default_for(style_type)
72
+ if style is None:
73
+ return None
74
+ return StyleFactory(style)
75
+
76
+ @property
77
+ def default_font(self) -> Font:
78
+ """Document-wide character defaults at the root of the style hierarchy.
79
+
80
+ Access creates missing default containers. More specific style or run
81
+ formatting can override these properties.
82
+ """
83
+ defaults = self._element.get_or_add_docDefaults().get_or_add_rPrDefault()
84
+ return Font(defaults)
85
+
86
+ def get_by_id(self, style_id: str | None, style_type: WD_STYLE_TYPE):
87
+ """Return the style of `style_type` matching `style_id`.
88
+
89
+ Returns the default for `style_type` if `style_id` is not found or is |None|, or
90
+ if the style having `style_id` is not of `style_type`.
91
+ """
92
+ if style_id is None:
93
+ return self.default(style_type)
94
+ return self._get_by_id(style_id, style_type)
95
+
96
+ def get_style_id(self, style_or_name, style_type):
97
+ """Return the id of the style corresponding to `style_or_name`, or |None| if
98
+ `style_or_name` is |None|.
99
+
100
+ If `style_or_name` is not a style object, the style is looked up using
101
+ `style_or_name` as a style name, raising |ValueError| if no style with that name
102
+ is defined. Raises |ValueError| if the target style is not of `style_type`.
103
+ """
104
+ if style_or_name is None:
105
+ return None
106
+ elif isinstance(style_or_name, BaseStyle):
107
+ return self._get_style_id_from_style(style_or_name, style_type)
108
+ else:
109
+ return self._get_style_id_from_name(style_or_name, style_type)
110
+
111
+ @property
112
+ def latent_styles(self):
113
+ """A |LatentStyles| object providing access to the default behaviors for latent
114
+ styles and the collection of |_LatentStyle| objects that define overrides of
115
+ those defaults for a particular named latent style."""
116
+ return LatentStyles(self._element.get_or_add_latentStyles())
117
+
118
+ def _get_by_id(self, style_id: str | None, style_type: WD_STYLE_TYPE):
119
+ """Return the style of `style_type` matching `style_id`.
120
+
121
+ Returns the default for `style_type` if `style_id` is not found or if the style
122
+ having `style_id` is not of `style_type`.
123
+ """
124
+ style = self._element.get_by_id(style_id) if style_id else None
125
+ if style is None or style.type != style_type:
126
+ return self.default(style_type)
127
+ return StyleFactory(style)
128
+
129
+ def _get_style_id_from_name(self, style_name: str, style_type: WD_STYLE_TYPE) -> str | None:
130
+ """Return the id of the style of `style_type` corresponding to `style_name`.
131
+
132
+ Returns |None| if that style is the default style for `style_type`. Raises
133
+ |ValueError| if the named style is not found in the document or does not match
134
+ `style_type`.
135
+ """
136
+ return self._get_style_id_from_style(self[style_name], style_type)
137
+
138
+ def _get_style_id_from_style(self, style: BaseStyle, style_type: WD_STYLE_TYPE) -> str | None:
139
+ """Id of `style`, or |None| if it is the default style of `style_type`.
140
+
141
+ Raises |ValueError| if style is not of `style_type`.
142
+ """
143
+ if style.type != style_type:
144
+ raise ValueError("assigned style is type %s, need type %s" % (style.type, style_type))
145
+ if style == self.default(style_type):
146
+ return None
147
+ return style.style_id