Skip to content

Instantly share code, notes, and snippets.

@marknagelberg
Created December 1, 2018 23:59
Show Gist options
  • Save marknagelberg/8895f9f60a3f696b4e24169f8134d33f to your computer and use it in GitHub Desktop.
Save marknagelberg/8895f9f60a3f696b4e24169f8134d33f to your computer and use it in GitHub Desktop.
Script part of blog post to load images into a Word Document
from docxtpl import DocxTemplate, InlineImage
import sys
import os
word_doc_template = sys.argv[1]
word_doc_w_images_loaded = sys.argv[2]
image_dirname = sys.argv[3]
# Don't want to overwrite your template with the loaded images version.
assert word_doc_template != word_doc_w_images_loaded
doc = DocxTemplate(word_doc_template)
images_to_load = {}
for image_filename in os.listdir(image_dirname):
# Remove extension to filename in dict key.
template_varname = image_filename.split('.')[0]
images_to_load[template_varname] = InlineImage(doc,·
os.path.join(image_dirname, image_filename))
doc.render(images_to_load)
doc.save(word_doc_w_images_loaded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment