@@ -21,6 +21,17 @@ class CT_P(OxmlBaseElement):
2121 """
2222 ``<w:p>`` element, containing the properties and text for a paragraph.
2323 """
24+ def __setattr__ (self , attr , value ):
25+ """
26+ Implement setter side of properties. Filters ``__setattr__`` messages
27+ to ObjectifiedElement base class to intercept messages intended for
28+ custom property setters.
29+ """
30+ if attr == 'style' :
31+ self ._set_style (value )
32+ else :
33+ super (CT_P , self ).__setattr__ (attr , value )
34+
2435 def add_r (self ):
2536 """
2637 Return a newly added CT_R (<w:r>) element.
@@ -53,7 +64,35 @@ def style(self):
5364 String contained in w:val attribute of <w:pPr><w:pStyle> child, or
5465 None if that element is not present.
5566 """
56- return self .pPr .style if hasattr (self , 'pPr' ) else None
67+ return self .pPr .style if self ._has_pPr else None
68+
69+ def _get_or_add_pPr (self ):
70+ """
71+ Return the pPr child element of this <w:p> element, adding a new one
72+ if one is not present.
73+ """
74+ if not self ._has_pPr :
75+ self .insert (0 , CT_PPr .new ())
76+ return self .pPr
77+
78+ @property
79+ def _has_pPr (self ):
80+ """
81+ Return True if this <w:p> element has a <w:pPr> child element, False
82+ otherwise.
83+ """
84+ return hasattr (self , 'pPr' )
85+
86+ def _set_style (self , style ):
87+ """
88+ Set style of this <w:p> element to *style*. If *style* is None,
89+ remove the style element. If the pPr element is empty after the
90+ operation, remove it.
91+ """
92+ pPr = self ._get_or_add_pPr ()
93+ pPr .style = style
94+ if pPr .countchildren () == 0 :
95+ self .remove (pPr )
5796
5897
5998class CT_PPr (OxmlBaseElement ):
0 commit comments