Skip to content
Snippets Groups Projects
Commit 57bf3ced authored by Carlos Vega's avatar Carlos Vega
Browse files

Fixes issue #235 and #234 in which merged templates raised an error when...

Fixes issue #235 and #234 in which merged templates raised an error when opened with Word and images were not properly rendered
parent afd6a660
No related branches found
No related tags found
1 merge request!167Fixes issue #235 and #234 in which merged templates raised an error when opened…
Pipeline #6562 passed
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment