Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def post_processing(self, docx_file):
if self.crc_to_new_media or self.crc_to_new_embedded:
if hasattr(docx_file, 'read'):
tmp_file = io.BytesIO()
DocxTemplate(docx_file).save(tmp_file)
tmp_file.seek(0)
docx_file.seek(0)
docx_file.truncate()
docx_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():
buf = zin.read(item.filename)
if ( item.filename.startswith('word/media/') and
item.CRC in self.crc_to_new_media ):
zout.writestr(item, self.crc_to_new_media[item.CRC])