title: Python-docx adding floating pictures
body: <p>Python-docx currently doesn't support inserting floating images, but I found the following code to be able to insert a &quot;behind text&quot; floating image, which works well.</p>
  <pre><code>from docx.oxml import parse_xml, register_element_cls
  from docx.oxml.ns import nsdecls
  from docx.oxml.shape import CT_Picture
  from docx.oxml.xmlchemy import BaseOxmlElement, OneAndOnlyOne
  
  class CT_Anchor(BaseOxmlElement):
      &quot;&quot;&quot;
      ```` element, container for a floating image.
      &quot;&quot;&quot;
      extent = OneAndOnlyOne('wp:extent')
      docPr = OneAndOnlyOne('wp:docPr')
      graphic = OneAndOnlyOne('a:graphic')
  
      @classmethod
      def new(cls, cx, cy, shape_id, pic, pos_x, pos_y):
          &quot;&quot;&quot;
          Return a new ```` element populated with the values passed
          as parameters.
          &quot;&quot;&quot;
          anchor = parse_xml(cls._anchor_xml(pos_x, pos_y))
          anchor.extent.cx = cx
          anchor.extent.cy = cy
          anchor.docPr.id = shape_id
          anchor.docPr.name = 'Picture %d' % shape_id
          anchor.graphic.graphicData.uri = (
              'http://schemas.openxmlformats.org/drawingml/2006/picture'
          )
          anchor.graphic.graphicData._insert_pic(pic)
          return anchor
  
      @classmethod
      def new_pic_anchor(cls, shape_id, rId, filename, cx, cy, pos_x, pos_y):
          &quot;&quot;&quot;
          Return a new `wp:anchor` element containing the `pic:pic` element
          specified by the argument values.
          &quot;&quot;&quot;
          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)
          anchor = cls.new(cx, cy, shape_id, pic, pos_x, pos_y)
          anchor.graphic.graphicData._insert_pic(pic)
          return anchor
  
      @classmethod
      def _anchor_xml(cls, pos_x, pos_y):
          return (
              '
              '           behindDoc=&quot;1&quot; locked=&quot;0&quot; layoutInCell=&quot;1&quot; allowOverlap=&quot;1&quot; \n'
              '           %s&gt;\n'
              '  \n'
              '  \n'
              '    %d\n'
              '  \n'
              '  \n'
              '    %d\n'
              '  \n'                    
              '  \n'
              '  \n'
              '  \n'
              '  \n'
              '    \n'
              '  \n'
              '  \n'
              '    \n'
              '  \n'
              '' % ( nsdecls('wp', 'a', 'pic', 'r'), int(pos_x), int(pos_y) )
          )
  
  
      def new_pic_anchor(part, image_descriptor, width, height, pos_x, pos_y):
          rId, image = part.get_or_add_image(image_descriptor)
          cx, cy = image.scaled_dimensions(width, height)
          shape_id, filename = part.next_id, image.filename    
          return CT_Anchor.new_pic_anchor(shape_id, rId, filename, cx, cy, pos_x, pos_y)
  
  
      def add_float_picture(p, image_path_or_stream, width=None, height=None, pos_x=0, pos_y=0):
          run = p.add_run()
          anchor = new_pic_anchor(run.part, image_path_or_stream, width, height, pos_x, pos_y)
          run._r.add_drawing(anchor)
  
  </code></pre>
  <p>If I want to insert &quot;Square Wrapping&quot; image, how should I change my code? I found someone who suggested &quot;changing wp:wrapNone/ to &lt;wp:wrapSquare wrapText = &quot;bothSides&quot;/&gt; will do the work for square text wrap.&quot; <a href="https://github.com/python-openxml/python-docx/issues/159" rel="nofollow noreferrer">https://github.com/python-openxml/python-docx/issues/159</a></p>
  <p>I tried it, but it doesn't work.</p>