Skip to content

Commit ec9e863

Browse files
author
Steve Canny
committed
style: add BaseStyle.delete()
1 parent 6c3ccd1 commit ec9e863

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

docx/oxml/styles.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class CT_Style(BaseOxmlElement):
4949
customStyle = OptionalAttribute('w:customStyle', ST_OnOff)
5050
del _tag_seq
5151

52+
def delete(self):
53+
"""
54+
Remove this `w:style` element from its parent `w:styles` element.
55+
"""
56+
self.getparent().remove(self)
57+
5258
@property
5359
def name_val(self):
5460
"""

docx/styles/style.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ def builtin(self):
4343
"""
4444
return not self._element.customStyle
4545

46+
def delete(self):
47+
"""
48+
Remove this style definition from the document. Note that calling
49+
this method does not remove or change the style applied to any
50+
document content. Content items having the deleted style will be
51+
rendered using the default style, as is any content with a style not
52+
defined in the document.
53+
"""
54+
self._element.delete()
55+
self._element = None
56+
4657
@property
4758
def name(self):
4859
"""

features/sty-delete-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Delete a style
44
I need a way to delete a style
55

66

7-
@wip
87
Scenario: Delete a style
98
Given a document having known styles
109
When I delete a style

tests/styles/test_style.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def it_knows_whether_its_a_builtin_style(self, builtin_get_fixture):
121121
style, expected_value = builtin_get_fixture
122122
assert style.builtin is expected_value
123123

124+
def it_can_delete_itself_from_the_document(self, delete_fixture):
125+
style, styles, expected_xml = delete_fixture
126+
style.delete()
127+
assert styles.xml == expected_xml
128+
assert style._element is None
129+
124130
# fixture --------------------------------------------------------
125131

126132
@pytest.fixture(params=[
@@ -133,6 +139,13 @@ def builtin_get_fixture(self, request):
133139
style = BaseStyle(element(style_cxml))
134140
return style, expected_value
135141

142+
@pytest.fixture
143+
def delete_fixture(self):
144+
styles = element('w:styles/w:style')
145+
style = BaseStyle(styles[0])
146+
expected_xml = xml('w:styles')
147+
return style, styles, expected_xml
148+
136149
@pytest.fixture(params=[
137150
('w:style', None),
138151
('w:style{w:styleId=Foobar}', 'Foobar'),

0 commit comments

Comments
 (0)