From 57bf3cedf85af861d50322dc3f3aadd4c0a5ff17 Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@.uni.lu>
Date: Tue, 25 Sep 2018 16:38:30 +0200
Subject: [PATCH] Fixes issue #235 and #234 in which merged templates raised an
 error when opened with Word and images were not properly rendered

---
 smash/web/docx_helper.py | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/smash/web/docx_helper.py b/smash/web/docx_helper.py
index f898ffc4..8a07243c 100644
--- a/smash/web/docx_helper.py
+++ b/smash/web/docx_helper.py
@@ -26,18 +26,23 @@ def process_file(path_to_docx, path_to_new_docx, changes_to_apply):
 
     doc.save(path_to_new_docx)
 
-
 def merge_files(files, path_to_new_docx):
-    merged_document = Document()
-
-    for index, input_file in enumerate(files):
-        sub_doc = Document(input_file)
-
-        # Don't add a page break if you've reached the last file.
+    '''
+    When combining templates into a new Document object, python-docx does not properly copy the information of the images, then Word is not able to locate the original content referred by the XML tag in the document. To fix this problem (see #234 ) the first file is loaded and the rest of templates are concatenated to this. This way, the original image content and rId match and the images are shown adequately.
+    See issue #235
+    '''
+    first_file = files[0]
+    files = files[1:]
+    merged_document = Document(first_file) #first file
+
+    if len(files) > 0:
+        merged_document.add_page_break()
+
+    for index, file in enumerate(files): #rest of files if any
+        sub_doc = Document(file)
         if index < len(files) - 1:
             sub_doc.add_page_break()
-
         for element in sub_doc.element.body:
             merged_document.element.body.append(element)
-
-    merged_document.save(path_to_new_docx)
+       
+    merged_document.save(path_to_new_docx)
\ No newline at end of file
-- 
GitLab