Skip to content

Commit 4f3bcc7

Browse files
author
Steve Canny
committed
txt: replace Paragraph.style setter
* place api-add-heading.feature back into WIP until Paragraph.style reimplementation is completed
1 parent aaf8da9 commit 4f3bcc7

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

docx/parts/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ def get_style(self, style_id, style_type):
7474
"""
7575
return self.styles.get_by_id(style_id, style_type)
7676

77+
def get_style_id(self, style_or_name, style_type):
78+
"""
79+
Return the style_id (|str|) of the style of *style_type* matching
80+
*style_or_name*. Returns |None| if the style resolves to the default
81+
style for *style_type* or if *style_or_name* is itself |None|. Raises
82+
if *style_or_name* is a style of the wrong type or names a style not
83+
present in the document.
84+
"""
85+
raise NotImplementedError
86+
7787
@lazyproperty
7888
def inline_shapes(self):
7989
"""

docx/text/paragraph.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ def style(self):
9090
return self.part.get_style(style_id, WD_STYLE_TYPE.PARAGRAPH)
9191

9292
@style.setter
93-
def style(self, style):
94-
self._p.style = None if style == 'Normal' else style
93+
def style(self, style_or_name):
94+
style_id = self.part.get_style_id(
95+
style_or_name, WD_STYLE_TYPE.PARAGRAPH
96+
)
97+
self._p.style = style_id
9598

9699
@property
97100
def text(self):

features/api-add-heading.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ Feature: Add a section heading with text
33
As a programmer using the basic python-docx API
44
I need a method to add a heading with its text in a single step
55

6+
7+
@wip
68
Scenario: Add a heading specifying only its text
79
Given a document
810
When I add a heading specifying only its text
911
Then the style of the last paragraph is 'Heading 1'
1012
And the last paragraph contains the heading text
1113

14+
15+
@wip
1216
Scenario Outline: Add a heading specifying level
1317
Given a document
1418
When I add a heading specifying level=<heading level>

tests/text/test_paragraph.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ def it_knows_its_paragraph_style(self, style_get_fixture):
6060

6161
def it_can_change_its_paragraph_style(self, style_set_fixture):
6262
paragraph, value, expected_xml = style_set_fixture
63+
6364
paragraph.style = value
65+
66+
paragraph.part.get_style_id.assert_called_once_with(
67+
value, WD_STYLE_TYPE.PARAGRAPH
68+
)
6469
assert paragraph._p.xml == expected_xml
6570

6671
def it_knows_the_text_it_contains(self, text_get_fixture):
@@ -192,22 +197,23 @@ def style_get_fixture(self, part_prop_):
192197
return paragraph, style_id, style_
193198

194199
@pytest.fixture(params=[
195-
('w:p', 'Heading1',
200+
('w:p', 'Heading 1', 'Heading1',
196201
'w:p/w:pPr/w:pStyle{w:val=Heading1}'),
197-
('w:p/w:pPr', 'Heading1',
202+
('w:p/w:pPr', 'Heading 1', 'Heading1',
198203
'w:p/w:pPr/w:pStyle{w:val=Heading1}'),
199-
('w:p/w:pPr/w:pStyle{w:val=Heading1}', 'Heading2',
204+
('w:p/w:pPr/w:pStyle{w:val=Heading1}', 'Heading 2', 'Heading2',
200205
'w:p/w:pPr/w:pStyle{w:val=Heading2}'),
201-
('w:p/w:pPr/w:pStyle{w:val=Heading1}', None,
206+
('w:p/w:pPr/w:pStyle{w:val=Heading1}', 'Normal', None,
202207
'w:p/w:pPr'),
203-
('w:p', None,
208+
('w:p', None, None,
204209
'w:p/w:pPr'),
205210
])
206-
def style_set_fixture(self, request):
207-
p_cxml, new_style_value, expected_cxml = request.param
211+
def style_set_fixture(self, request, part_prop_):
212+
p_cxml, value, style_id, expected_cxml = request.param
208213
paragraph = Paragraph(element(p_cxml), None)
214+
part_prop_.return_value.get_style_id.return_value = style_id
209215
expected_xml = xml(expected_cxml)
210-
return paragraph, new_style_value, expected_xml
216+
return paragraph, value, expected_xml
211217

212218
@pytest.fixture(params=[
213219
('w:p', ''),

0 commit comments

Comments
 (0)