title: How do I insert multiple images into a table using python's docxtpl?
body: <p>I am working on a process to automate the generation of a report in Word. I have a table in my template and I would like to insert images into some cells in the table. If I insert only one image into the table, it works perfectly. However, if I insert more than one image, the output file gets corrupted. While opening the file I am getting an error says &quot;Word found unreadable content in XXXX. Do you want to recover?...&quot;</p>
  <p>My table in the template.docx looks like this:</p>
  <p><a href="https://i.stack.imgur.com/qCPsA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qCPsA.png" alt="template table screenshot" /></a></p>
  <pre><code>import psycopg2
  from docxtpl import DocxTemplate, InlineImage
  from docx2pdf import convert
  import os
  from datetime import date
  
  
  # Report template and output paths
  report_name_docx = date.today().strftime(&quot;%Y%m%d&quot;) + 'xxx_report.docx'
  report_template = &quot;C:\\workspace\\xxx_template.docx&quot;
  report_output = os.path.join(&quot;C:\\workspace\\template\\&quot;,report_name_docx)
  
  id = int(input('Enter please id:'))
  
  # Connect and query tables in Postgresql
  try:
      connection = psycopg2.connect(user='xxx',
                                    password='xxx',
                                    host=&quot;xxx&quot;,
                                    port=&quot;xxx&quot;,
                                    database=&quot;xxx&quot;)
      cursor = connection.cursor()
  
      # Fetch records 
      postgreSQL_select_Query1 = f&quot;select * from xxx where id = {id}&quot;
      cursor.execute(postgreSQL_select_Query1)
      apsfr_records = cursor.fetchall()
  
      # Get name of the apsfr
      for row in apsfr_records:
          name = row[3]
  
    
  
  except (Exception, psycopg2.Error) as error:
      print(f&quot;Error while connecting to PostgreSQL {error}&quot;)
  
  finally:
      # closing database connection.
      if (connection):
          cursor.close()
          connection.close()
          print(&quot;PostgreSQL connection is closed&quot;)
  
  # Don't want to overwrite your template with the loaded images version.
  assert report_template!=report_output
  
  doc = DocxTemplate(report_template)
  
  context_to_load = {
      'name': name
  }
  
  context_to_load['image1'] = InlineImage(doc, os.path.join(image_dirname, &quot;umgesetztdouble.png&quot;))
  context_to_load['Image2'] = InlineImage(doc, os.path.join(image_dirname, &quot;line.png&quot;))
  
  
  # load the context to the word template
  doc.render(context_to_load)
  doc.save(report_output)
  
  print(&quot;-----&quot;)
  print('----- FINITO')
  </code></pre>