Issue- #200
This commit is contained in:
parent
20819a7368
commit
a021c517f5
@ -5,6 +5,7 @@ Created : 2015-03-12
|
|||||||
@author: Eric Lapouyade
|
@author: Eric Lapouyade
|
||||||
'''
|
'''
|
||||||
import functools
|
import functools
|
||||||
|
import io
|
||||||
|
|
||||||
__version__ = '0.5.18'
|
__version__ = '0.5.18'
|
||||||
|
|
||||||
@ -450,13 +451,20 @@ class DocxTemplate(object):
|
|||||||
crc = self.get_file_crc(src_file)
|
crc = self.get_file_crc(src_file)
|
||||||
self.crc_to_new_embedded[crc] = fh.read()
|
self.crc_to_new_embedded[crc] = fh.read()
|
||||||
|
|
||||||
def post_processing(self,docx_filename):
|
def post_processing(self, docx_file):
|
||||||
if self.crc_to_new_media or self.crc_to_new_embedded:
|
if self.crc_to_new_media or self.crc_to_new_embedded:
|
||||||
backup_filename = '%s_docxtpl_before_replace_medias' % docx_filename
|
|
||||||
os.rename(docx_filename,backup_filename)
|
|
||||||
|
|
||||||
with zipfile.ZipFile(backup_filename) as zin:
|
if hasattr(docx_file, 'read'):
|
||||||
with zipfile.ZipFile(docx_filename, 'w') as zout:
|
tmp_file = io.BytesIO()
|
||||||
|
Document(docx_file).save(tmp_file)
|
||||||
|
tmp_file.seek(0)
|
||||||
|
|
||||||
|
else:
|
||||||
|
tmp_file = '%s_docxtpl_before_replace_medias' % docx_file
|
||||||
|
os.rename(docx_file, tmp_file)
|
||||||
|
|
||||||
|
with zipfile.ZipFile(tmp_file) as zin:
|
||||||
|
with zipfile.ZipFile(docx_file, 'w') as zout:
|
||||||
for item in zin.infolist():
|
for item in zin.infolist():
|
||||||
buf = zin.read(item.filename)
|
buf = zin.read(item.filename)
|
||||||
if ( item.filename.startswith('word/media/') and
|
if ( item.filename.startswith('word/media/') and
|
||||||
@ -468,7 +476,10 @@ class DocxTemplate(object):
|
|||||||
else:
|
else:
|
||||||
zout.writestr(item, buf)
|
zout.writestr(item, buf)
|
||||||
|
|
||||||
os.remove(backup_filename)
|
if not hasattr(tmp_file, 'read'):
|
||||||
|
os.remove(tmp_file)
|
||||||
|
if hasattr(docx_file, 'read'):
|
||||||
|
docx_file.seek(0)
|
||||||
|
|
||||||
def pre_processing(self):
|
def pre_processing(self):
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ from docxtpl import DocxTemplate
|
|||||||
import io
|
import io
|
||||||
|
|
||||||
DEST_FILE = 'output/header_footer_image_file_obj.docx'
|
DEST_FILE = 'output/header_footer_image_file_obj.docx'
|
||||||
|
DEST_FILE2 = 'output/header_footer_image_file_obj2.docx'
|
||||||
|
|
||||||
tpl=DocxTemplate('templates/header_footer_image_tpl.docx')
|
tpl=DocxTemplate('templates/header_footer_image_tpl.docx')
|
||||||
|
|
||||||
@ -20,4 +21,15 @@ dummy_pic = io.BytesIO(open('templates/dummy_pic_for_header.png', 'rb').read())
|
|||||||
new_image = io.BytesIO(open('templates/python.png', 'rb').read())
|
new_image = io.BytesIO(open('templates/python.png', 'rb').read())
|
||||||
tpl.replace_media(dummy_pic, new_image)
|
tpl.replace_media(dummy_pic, new_image)
|
||||||
tpl.render(context)
|
tpl.render(context)
|
||||||
tpl.save(DEST_FILE)
|
tpl.save(DEST_FILE)
|
||||||
|
|
||||||
|
tpl = DocxTemplate('templates/header_footer_image_tpl.docx')
|
||||||
|
dummy_pic.seek(0)
|
||||||
|
new_image.seek(0)
|
||||||
|
tpl.replace_media(dummy_pic, new_image)
|
||||||
|
tpl.render(context)
|
||||||
|
|
||||||
|
file_obj = io.BytesIO()
|
||||||
|
tpl.save(file_obj)
|
||||||
|
file_obj.seek(0)
|
||||||
|
DocxTemplate(file_obj).save(DEST_FILE2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user