Skip to content

Commit 99b8a66

Browse files
author
Steve Canny
committed
oxml: add CT_PPr.style getter
1 parent 80390ff commit 99b8a66

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from docx.oxml.base import register_custom_element_class
1616
from docx.oxml.parts import CT_Body, CT_Document
17-
from docx.oxml.text import CT_P, CT_R, CT_Text
17+
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_Text
1818

1919

2020
# ===========================================================================
@@ -24,5 +24,6 @@
2424
register_custom_element_class('w:body', CT_Body)
2525
register_custom_element_class('w:document', CT_Document)
2626
register_custom_element_class('w:p', CT_P)
27+
register_custom_element_class('w:pPr', CT_PPr)
2728
register_custom_element_class('w:r', CT_R)
2829
register_custom_element_class('w:t', CT_Text)

docx/oxml/text.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(CT_R).
1313
"""
1414

15-
from docx.oxml.base import nsdecls, OxmlBaseElement, oxml_fromstring
15+
from docx.oxml.base import nsdecls, OxmlBaseElement, oxml_fromstring, qn
1616

1717

1818
class CT_P(OxmlBaseElement):
@@ -46,6 +46,21 @@ def r_elms(self):
4646
return tuple([r for r in self.r])
4747

4848

49+
class CT_PPr(OxmlBaseElement):
50+
"""
51+
``<w:pPr>`` element, containing the properties for a paragraph.
52+
"""
53+
@property
54+
def style(self):
55+
"""
56+
String contained in <w:pStyle> child, or None if that element is not
57+
present.
58+
"""
59+
if not hasattr(self, 'pStyle'):
60+
return None
61+
return self.pStyle.get(qn('w:val'))
62+
63+
4964
class CT_R(OxmlBaseElement):
5065
"""
5166
``<w:r>`` element, containing the properties and text for a run.

tests/oxml/test_text.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from docx.oxml.text import CT_P, CT_R, CT_Text
1313

14-
from ..unitdata import a_p, a_t, an_r
14+
from ..unitdata import a_p, a_pPr, a_t, an_r
1515

1616

1717
class DescribeCT_P(object):
@@ -36,6 +36,19 @@ def it_can_add_an_r_to_itself(self):
3636
assert isinstance(r, CT_R)
3737

3838

39+
class DescribeCT_PPr(object):
40+
41+
def it_knows_the_paragraph_style(self):
42+
cases = (
43+
(a_pPr(), None),
44+
(a_pPr().with_style('foobar'), 'foobar'),
45+
)
46+
for builder, expected_value in cases:
47+
print builder.with_nsdecls().xml
48+
pPr = builder.with_nsdecls().element
49+
assert pPr.style == expected_value
50+
51+
3952
class DescribeCT_R(object):
4053

4154
def it_can_construct_a_new_r_element(self):

tests/unitdata.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,39 @@ def xml(self):
147147
return xml
148148

149149

150+
class CT_PPrBuilder(BaseBuilder):
151+
"""
152+
Test data builder for a CT_PPr (<w:pPr>) XML element that appears as a
153+
child of a <w:p> element in a document.xml file.
154+
"""
155+
def __init__(self):
156+
"""Establish instance variables with default values"""
157+
super(CT_PPrBuilder, self).__init__()
158+
self._pStyle = None
159+
160+
@property
161+
def is_empty(self):
162+
return self._pStyle is None
163+
164+
def with_style(self, style='foobar'):
165+
"""Add pStyle child with inner text *style*"""
166+
self._pStyle = '<w:pStyle w:val="%s"/>' % style
167+
return self
168+
169+
@property
170+
def xml(self):
171+
"""Return element XML based on attribute settings"""
172+
indent = ' ' * self._indent
173+
if self.is_empty:
174+
xml = '%s<w:pPr%s/>\n' % (indent, self._nsdecls)
175+
else:
176+
xml = '%s<w:pPr%s>\n' % (indent, self._nsdecls)
177+
if self._pStyle:
178+
xml += '%s%s\n' % (indent+' ', self._pStyle)
179+
xml += '%s</w:pPr>\n' % indent
180+
return xml
181+
182+
150183
class CT_RBuilder(BaseBuilder):
151184
"""
152185
Test data builder for a CT_R (<w:r>) XML element that appears within the
@@ -229,6 +262,11 @@ def a_p():
229262
return CT_PBuilder()
230263

231264

265+
def a_pPr():
266+
"""Return a CT_PPrBuilder instance"""
267+
return CT_PPrBuilder()
268+
269+
232270
def a_t(text):
233271
"""Return a CT_TextBuilder instance"""
234272
return CT_TextBuilder(text)

0 commit comments

Comments
 (0)