From fbb930d3eafb3a580ecd16381bd3aa79a7fea664 Mon Sep 17 00:00:00 2001 From: ericdufresne Date: Mon, 3 Jun 2019 11:34:16 -0400 Subject: [PATCH] Provide better support for file-like objects with replace-media --- docxtpl/__init__.py | 3 ++- tests/header_footer_image_file_obj.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index ec1e27a..115722b 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -456,8 +456,9 @@ class DocxTemplate(object): if hasattr(docx_file, 'read'): tmp_file = io.BytesIO() - Document(docx_file).save(tmp_file) + DocxTemplate(docx_file).save(tmp_file) tmp_file.seek(0) + docx_file.seek(0) else: tmp_file = '%s_docxtpl_before_replace_medias' % docx_file diff --git a/tests/header_footer_image_file_obj.py b/tests/header_footer_image_file_obj.py index bdc25e9..4337640 100644 --- a/tests/header_footer_image_file_obj.py +++ b/tests/header_footer_image_file_obj.py @@ -32,4 +32,6 @@ tpl.render(context) file_obj = io.BytesIO() tpl.save(file_obj) file_obj.seek(0) -DocxTemplate(file_obj).save(DEST_FILE2) +with open(DEST_FILE2, 'wb') as f: + f.write(file_obj.read()) +