66)
77
88from pydocx .models import XmlModel , XmlCollection , XmlChild
9+ from pydocx .openxml .wordprocessing .bookmark import Bookmark
10+ from pydocx .openxml .wordprocessing .deleted_run import DeletedRun
911from pydocx .openxml .wordprocessing .hyperlink import Hyperlink
12+ from pydocx .openxml .wordprocessing .inserted_run import InsertedRun
1013from pydocx .openxml .wordprocessing .paragraph_properties import ParagraphProperties # noqa
1114from pydocx .openxml .wordprocessing .run import Run
12- from pydocx .openxml .wordprocessing .tab_char import TabChar
13- from pydocx .openxml .wordprocessing .text import Text
14- from pydocx .openxml .wordprocessing .smart_tag_run import SmartTagRun
15- from pydocx .openxml .wordprocessing .inserted_run import InsertedRun
16- from pydocx .openxml .wordprocessing .deleted_run import DeletedRun
1715from pydocx .openxml .wordprocessing .sdt_run import SdtRun
1816from pydocx .openxml .wordprocessing .simple_field import SimpleField
19- from pydocx .openxml .wordprocessing .bookmark import Bookmark
17+ from pydocx .openxml .wordprocessing .smart_tag_run import SmartTagRun
18+ from pydocx .openxml .wordprocessing .tab_char import TabChar
19+ from pydocx .openxml .wordprocessing .text import Text
20+ from pydocx .openxml .wordprocessing .table_cell import TableCell
2021from pydocx .util .memoize import memoized
2122
2223
@@ -231,27 +232,62 @@ def get_spacing(self):
231232 """
232233 results = {
233234 'line' : None ,
234- 'after' : None
235+ 'after' : None ,
236+ 'before' : None ,
237+ 'contextual_spacing' : False ,
238+ 'parent_style' : None
235239 }
236240
237- default_properties_spacing = self .default_doc_styles .paragraph .properties
238- no_spacing_properties = not self .properties or self .properties .no_spacing
241+ # Get the paragraph_properties from the parent styles
242+ style_paragraph_properties = None
243+ for style in self .get_style_chain_stack ():
244+ if style .paragraph_properties :
245+ style_paragraph_properties = style .paragraph_properties
246+ break
247+
248+ if style_paragraph_properties :
249+ results ['contextual_spacing' ] = bool (style_paragraph_properties .contextual_spacing )
250+
251+ default_paragraph_properties = None
252+ if self .default_doc_styles and self .default_doc_styles .paragraph :
253+ default_paragraph_properties = self .default_doc_styles .paragraph .properties
239254
240- if not default_properties_spacing and no_spacing_properties :
255+ # Spacing properties can be defined in multiple places and we need to get some
256+ # kind of order of check
257+ properties_order = [None , None , None ]
258+ if self .properties :
259+ properties_order [0 ] = self .properties
260+ if isinstance (self .parent , TableCell ):
261+ properties_order [1 ] = self .parent .parent_table .get_paragraph_properties ()
262+ if not self .properties or not self .properties .spacing_properties :
263+ properties_order [2 ] = default_paragraph_properties
264+
265+ spacing_properties = None
266+ contextual_spacing = None
267+
268+ for properties in properties_order :
269+ if spacing_properties is None :
270+ spacing_properties = getattr (properties , 'spacing_properties' , None )
271+ if contextual_spacing is None :
272+ contextual_spacing = getattr (properties , 'contextual_spacing' , None )
273+
274+ if not spacing_properties :
241275 return results
242276
243- if no_spacing_properties :
244- properties = default_properties_spacing
245- else :
246- properties = self .properties
277+ if contextual_spacing is not None :
278+ results ['contextual_spacing' ] = bool (contextual_spacing )
247279
248- spacing_line = properties . to_int ( 'spacing_line' )
249- spacing_after = properties .to_int ( 'spacing_after' )
280+ if self . properties :
281+ results [ 'parent_style' ] = self . properties .parent_style
250282
251- if default_properties_spacing and spacing_line is None \
252- and bool (properties .spacing_after_auto_spacing ):
283+ spacing_line = spacing_properties .to_int ('line' )
284+ spacing_after = spacing_properties .to_int ('after' )
285+ spacing_before = spacing_properties .to_int ('before' )
286+
287+ if default_paragraph_properties and spacing_line is None \
288+ and bool (spacing_properties .after_auto_spacing ):
253289 # get the spacing_line from the default definition
254- spacing_line = default_properties_spacing . to_int ('spacing_line ' )
290+ spacing_line = default_paragraph_properties . spacing_properties . to_int ('line ' )
255291
256292 if spacing_line :
257293 line = spacing_line / 240.0
@@ -262,4 +298,7 @@ def get_spacing(self):
262298 if spacing_after is not None :
263299 results ['after' ] = spacing_after
264300
301+ if spacing_before is not None :
302+ results ['before' ] = spacing_before
303+
265304 return results
0 commit comments