Skip to content
Prev Previous commit
Next Next commit
Add margin
  • Loading branch information
jtrain committed May 1, 2017
commit a162814803eb7fee1afabaa0384cd4935512f686
4 changes: 2 additions & 2 deletions docx/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def add_paragraph(self, text='', style=None):

def add_picture(
self, image_path_or_stream, width=None, height=None,
position=None, wrap=None
position=None, margin=None, wrap=None
):
"""
Return a new picture shape added in its own paragraph at the end of
Expand All @@ -80,7 +80,7 @@ def add_picture(
"""
run = self.add_paragraph().add_run()
return run.add_picture(
image_path_or_stream, width, height, position, wrap
image_path_or_stream, width, height, position, margin, wrap
)

def add_section(self, start_type=WD_SECTION.NEW_PAGE):
Expand Down
22 changes: 16 additions & 6 deletions docx/oxml/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CT_Inline(BaseOxmlElement):
graphic = OneAndOnlyOne('a:graphic')

@classmethod
def new(cls, cx, cy, shape_id, pic, position=None, wrap=None):
def new(cls, cx, cy, shape_id, pic, position=None, margin=None, wrap=None):
"""
Return a new ``<wp:inline>`` element populated with the values passed
as parameters.
Expand All @@ -76,22 +76,24 @@ def new(cls, cx, cy, shape_id, pic, position=None, wrap=None):

@classmethod
def new_pic_inline(
cls, shape_id, rId, filename, cx, cy, position=None, wrap=None):
cls, shape_id, rId, filename, cx, cy,
position=None, margin=None, wrap=None):
"""
Return a new `wp:inline` element containing the `pic:pic` element
specified by the argument values.
"""
pic_id = 0 # Word doesn't seem to use this, but does not omit it
pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
inline = cls.new(cx, cy, shape_id, pic, position, wrap)
inline = cls.new(cx, cy, shape_id, pic, position, margin, wrap)
inline.graphic.graphicData._insert_pic(pic)
return inline

@classmethod
def new_pic(
cls, shape_id, rId, filename, cx, cy, position=None, wrap=None):
cls, shape_id, rId, filename, cx, cy,
position=None, margin=None, wrap=None):
return cls.new_pic_inline(
shape_id, rId, filename, cx, cy, position, wrap
shape_id, rId, filename, cx, cy, position, margin, wrap
)

@classmethod
Expand Down Expand Up @@ -121,7 +123,7 @@ class CT_Anchor(CT_Inline):
wrapSquare = ZeroOrOne('wp:wrapSquare')

@classmethod
def new(cls, cx, cy, shape_id, pic, position, wrap=None):
def new(cls, cx, cy, shape_id, pic, position, margin=None, wrap=None):
"""
Return a new ``<wp:inline>`` element populated with the values passed
as parameters.
Expand All @@ -138,8 +140,16 @@ def new(cls, cx, cy, shape_id, pic, position, wrap=None):
positionH, positionV = position
anchor.positionH.getchildren()[0].text = positionH
anchor.positionV.getchildren()[0].text = positionV

if margin is not None:
anchor.distT = margin.get('top', 0)
anchor.distR = margin.get('right', 0)
anchor.distB = margin.get('bottom', 0)
anchor.distL = margin.get('left', 0)

if wrap is not None:
anchor.wrapSquare.set('wrapText', wrap)

return anchor

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions docx/parts/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def new_pic_inline(self, image_descriptor, width, height):
return self.new_pic(image_descriptor, width, height)

def new_pic(
self, image_descriptor, width, height, position=None, wrap=None):
self, image_descriptor, width, height,
position=None, margin=None, wrap=None):
"""
Return a new `w:inline` or `w:anchor` element containing the image
specified by *image_descriptor* and scaled based on the values of
Expand All @@ -109,7 +110,7 @@ def new_pic(
else:
ShapeType = CT_Anchor
return ShapeType.new_pic(
shape_id, rId, filename, cx, cy, position, wrap
shape_id, rId, filename, cx, cy, position, margin, wrap
)

@property
Expand Down
4 changes: 2 additions & 2 deletions docx/text/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add_break(self, break_type=WD_BREAK.LINE):

def add_picture(
self, image_path_or_stream, width=None, height=None,
position=None, wrap=None
position=None, margin=None, wrap=None
):
"""
Return an |InlineShape| instance containing the image identified by
Expand All @@ -65,7 +65,7 @@ def add_picture(
false if floated.
"""
image = self.part.new_pic(
image_path_or_stream, width, height, position, wrap
image_path_or_stream, width, height, position, margin, wrap
)
self._r.add_drawing(image)

Expand Down