Skip to content

Commit 45dd568

Browse files
author
Steve Canny
committed
style: add LatentStyles on/off property getters
1 parent f9cacf3 commit 45dd568

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

docx/oxml/styles.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@ class CT_LatentStyles(BaseOxmlElement):
3939
lsdException = ZeroOrMore('w:lsdException', successors=())
4040

4141
count = OptionalAttribute('w:count', ST_DecimalNumber)
42+
defLockedState = OptionalAttribute('w:defLockedState', ST_OnOff)
43+
defQFormat = OptionalAttribute('w:defQFormat', ST_OnOff)
44+
defSemiHidden = OptionalAttribute('w:defSemiHidden', ST_OnOff)
4245
defUIPriority = OptionalAttribute('w:defUIPriority', ST_DecimalNumber)
46+
defUnhideWhenUsed = OptionalAttribute('w:defUnhideWhenUsed', ST_OnOff)
47+
48+
def bool_prop(self, attr_name):
49+
"""
50+
Return the boolean value of the attribute having *attr_name*, or
51+
|False| if not present.
52+
"""
53+
value = getattr(self, attr_name)
54+
if value is None:
55+
return False
56+
return value
4357

4458
def get_by_name(self, name):
4559
"""

docx/styles/latent.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,42 @@ def default_priority(self):
4444
"""
4545
return self._element.defUIPriority
4646

47+
@property
48+
def default_to_hidden(self):
49+
"""
50+
Boolean specifying whether the default behavior for latent styles is
51+
to be hidden. A hidden style does not appear in the recommended list
52+
or in the style gallery.
53+
"""
54+
return self._element.bool_prop('defSemiHidden')
55+
56+
@property
57+
def default_to_locked(self):
58+
"""
59+
Boolean specifying whether the default behavior for latent styles is
60+
to be locked. A locked style does not appear in the styles panel or
61+
the style gallery and cannot be applied to document content. This
62+
behavior is only active when formatting protection is turned on for
63+
the document (via the Developer menu).
64+
"""
65+
return self._element.bool_prop('defLockedState')
66+
67+
@property
68+
def default_to_quick_style(self):
69+
"""
70+
Boolean specifying whether the default behavior for latent styles is
71+
to appear in the style gallery when not hidden.
72+
"""
73+
return self._element.bool_prop('defQFormat')
74+
75+
@property
76+
def default_to_unhide_when_used(self):
77+
"""
78+
Boolean specifying whether the default behavior for latent styles is
79+
to be unhidden when first applied to content.
80+
"""
81+
return self._element.bool_prop('defUnhideWhenUsed')
82+
4783
@property
4884
def load_count(self):
4985
"""

features/sty-latent-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Get and set latent style properties
44
I need a set of read/write latent style properties
55

66

7-
@wip
87
Scenario Outline: Get default latent style properties
98
Given a latent styles object with known defaults
109
Then latent_styles.<prop-name> is <value>

tests/styles/test_latent.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,32 @@ def it_knows_its_load_count(self, count_get_fixture):
4747
latent_styles, expected_value = count_get_fixture
4848
assert latent_styles.load_count == expected_value
4949

50+
def it_knows_its_boolean_properties(self, bool_prop_get_fixture):
51+
latent_styles, prop_name, expected_value = bool_prop_get_fixture
52+
actual_value = getattr(latent_styles, prop_name)
53+
assert actual_value == expected_value
54+
5055
# fixture --------------------------------------------------------
5156

57+
@pytest.fixture(params=[
58+
('w:latentStyles', 'default_to_hidden', False),
59+
('w:latentStyles', 'default_to_locked', False),
60+
('w:latentStyles', 'default_to_quick_style', False),
61+
('w:latentStyles', 'default_to_unhide_when_used', False),
62+
('w:latentStyles{w:defSemiHidden=1}',
63+
'default_to_hidden', True),
64+
('w:latentStyles{w:defLockedState=0}',
65+
'default_to_locked', False),
66+
('w:latentStyles{w:defQFormat=on}',
67+
'default_to_quick_style', True),
68+
('w:latentStyles{w:defUnhideWhenUsed=false}',
69+
'default_to_unhide_when_used', False),
70+
])
71+
def bool_prop_get_fixture(self, request):
72+
latentStyles_cxml, prop_name, expected_value = request.param
73+
latent_styles = LatentStyles(element(latentStyles_cxml))
74+
return latent_styles, prop_name, expected_value
75+
5276
@pytest.fixture(params=[
5377
('w:latentStyles', None),
5478
('w:latentStyles{w:count=42}', 42),

0 commit comments

Comments
 (0)