python-docx does not add picture

python-docx add_picture
python-docx add picture to table cell
python-docx add image to header
python-docx replace image
python-docx iterate over table
python-docx picture size
python-docx table
python-docx watermark

I'm trying to insert a picture into a Word document using python-docx but running into errors.

The code is simply:

document.add_picture("test.jpg", width = Cm(2.0))

From looking at the python-docx documentation I can see that the following XML should be generated:

<pic:pic xmlns:pic="">
    <pic:cNvPr id="1" name="python-powered.png"/>
    <a:blip r:embed="rId7"/>
      <a:off x="0" y="0"/>
      <a:ext cx="859536" cy="343814"/>
    <a:prstGeom prst="rect"/>

This does in fact get generated in my document.xml file. (When unzipping the docx file). However looking into the OOXML format I can see that the image should also be saved under the media folder and the relationship should be mapped in word/_rels/document.xml:

<Relationship Id="rId20"

None of this is happens however, and when I open the Word document I'm met with a "The picture can't be displayed" placeholder.

Can anyone help me understand what is going on?

It looks like the image is not embedded the way it should be and I need to insert it in the media folder and add the mapping for it, however as a well documented feature this should be working as expected.


Testing it out with an empty docx file that image does get added as expected which leads me to believe it might have something to do with the python-docx-template library. (

It uses python-docx and jinja to allow templating capabilities but runs and works the same way python-docx should. I added the image to a subdoc which then gets inserted into a full document at a given place.

A sample code can be seen below (from

from docxtpl import DocxTemplate
from docx.shared import Inches


sd = tpl.new_subdoc()
sd.add_paragraph('A picture :')
sd.add_picture('test_files/python_logo.png', width=Inches(1.25))

context = {
    'mysubdoc' : sd,


I'll keep this up in case anyone else manages to make the same mistake as I did :) I managed to debug it in the end.

The problem was in how I used the python-docx-template library. I opened up a DocxTemplate like so:

    report_output = DocxTemplate(template_path)

But I accidentally opened it up twice. Instead of passing the template to a function, when working with it, I passed a path to it and opened it again when creating subdocs and building them.

def DoThings(data,template_path):
    doc = DocxTemplate(template_path)
    temp_finding = doc.new_subdoc()

Finally after I had the subdocs built, I rendered the first template which seemed to work fine for paragraphs and such but I'm guessing the images were added to the "second" opened template and not to the first one that I was actually rendering. After passing the template to the function it started working as expected!

Picture — python-docx 0.8.10 documentation, Picture�. Word allows a picture to be placed in a graphical object container, either of the minimum XML that must be inserted to add a working picture to a document: unique DrawingML object id (document-wide, pretty sure it's just the part)� just started learning python, now I need help with the python docx function. I'm using python v3.5.1. That's the code I'd like to run from a .py file: from docx import Document document = Document paragraph = document.add_paragraph('I am adding a new paragraph here.')'test-thu18feb-b.docx')

to add pictures, headings, paragraphs to existing document:

doc = Document(full_path) # open an existing document with existing styles
for row in tableData:   # list from the json api ... 
    print ('row {}'.format(row))
    level = row['level']
    levelStyle = 'Heading ' + str(level)
    title = row['title']
    heading = doc.add_heading( title , level) = doc.styles[levelStyle]
    p = doc.add_paragraph(row['description'])
    if row['img_http_path']:
        ip = doc.add_paragraph()
        r = ip.add_run()
        r.add_picture(row['img_http_path'], width = Cm(15.0))

Understanding pictures and other shapes — python-docx 0.8.10 , A picture is a shape that can appear in either the text or drawing layer. When it appears in the Often, a picture is placed in a paragraph by itself, but this is not required. It can have text before Floating pictures can be added. If you have an � from io import BytesIO import requests from docx import Document from docx.shared import Inches response = requests.get(your_image_url) # no need to add stream=True # Access the response body as bytes # then convert it to in-memory binary stream using `BytesIO` binary_img = BytesIO(response.content) document = Document() # `add_picture` supports image path or stream, we use stream document.add_picture(binary_img, width=Inches(2))'demo.docx')

I came acrossed with this problem and it was solved after the parameter width=(1.0) in method add_picture removed.

when parameter width=(1.0) was added, I could not see the pic in test.docx

so, it MIGHT BE resulted from an unappropriate size was set to the picture,

Quickstart — python-docx 0.8.10 documentation, This can be handy to start a relatively short document that doesn't have a separate title Word lets you place an image in a document using the Insert > Photo� A perfect use for a tool such as python-docx is to archive a large number of images in an automated fashion. Unfortunately, floating images is a poor way of doing this and inserting the images in table cells is my preferred route. I had hoped that python-docx would allow me to do that (and it was, in fact, the only reason I needed it for).

hidden inline images � Issue #667 � python-openxml/python-docx , I am adding an inline image to a templated document by: doc In the template I am working from, the image does not actually show on the� python-docx 0.8.10 documentation This XML represents the working hypothesis of the minimum XML that must be inserted to add a working picture to a document: <pic:

Python docx operation word, insert the picture at the specified location, Table positioning is more accurate, but sometimes our word is not a table at all, so we can only locate paragraphs. However, the picture inserted� python-docx does not automatically set any of the document core properties other than to add a core properties part to a presentation that doesn’t have one (very uncommon). If python-docx adds a core properties part, it contains default values for the title, last_modified_by, revision, and modified properties.

Create a simple docx file usign python (Odoo): python-docx library, I will show how to add the images, header, footer, etc in the .docx file. In order to start with the python-docx, you have to install the library, you� Word lets you place an image in a document using the Insert > Photo > Picture from file menu item. Here’s how to do it in python-docx : document . add_picture ( 'image-filename.png' )